【NOIP2013提高组】花匠

这里写图片描述

题目很明显是要找一个锯齿形的序列。对于连续下降或上升的子序列,只能留下其中任意一个才能形成,于是可以直接找拐点,拐点个数+1就是最长锯齿序列的长度。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<cctype>
#include<vector>
using namespace std;
int n,a[100005]={0};

void read(int &x)
{
    x=0;
    bool ok=0;
    char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-')
    {
        ch=getchar();
    }
    while((ch>='0'&&ch<='9')||ch=='-')
    {
        if(ch=='-') ok=1;
        else x=x*10+ch-'0';
        ch=getchar();
    }
    if(ok) x=-x;
}

void in()
{
    read(n);
    for(int i=1;i<=n;i++) read(a[i]);
}

void task()
{
    int ans1=1,ans2=1;
    bool ok=0;
    for(int i=2;i<=n;i++)
    {
        if(!ok&&a[i]>a[i-1])
        {
            ans1++;
            ok=1;
        }
        if(ok&&a[i]<a[i-1])
        {
            ans1++;
            ok=0;
        }
    }
    ok=1;
    for(int i=2;i<=n;i++)
    {
        if(!ok&&a[i]>a[i-1])
        {
            ans2++;
            ok=1;
        }
        if(ok&&a[i]<a[i-1])
        {
            ans2++;
            ok=0;
        }
    }
    printf("%d",max(ans1,ans2));
}

int main()
{
    freopen("in.txt","r",stdin);
    in();
    task();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值