洛谷 1970 [NOIP2013] 花匠 贪心

题目:

https://www.luogu.org/problem/show?pid=1970

题目大意:
找最长的波动序列;
对于序列上的ai,满足ai-1< ai , ai >ai+1;

所以分两种情况讨论:
1.第一个数作为波谷;
2.第一个数作为波峰;

贪心;
时间: O(n);
空间: O(1);

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int MAXN=2000001;
int ans1=1,ans2=1,n,h,x;

int read()
{
    int fla=1,ss=0;
    char c=getchar();
    while(c<'0' || c>'9') 
    {
        c=getchar();
        if(c=='-') fla=-1;
    }
    while(c>='0' && c<='9')
    {
        ss=ss*10+c-'0';
        c=getchar();
    }
    return ss*fla;
}


void solve()
{

    n=read(),h=read();
    for(int i=2;i<=n;i++)
    {
        x=read();
        if(x>h) ans1=max(ans2+1,ans1);
        else if(x<h) ans2=max(ans1+1,ans2);
        h=x;
    }
    cout<<max(ans1,ans2)<<endl;
    return;
}


int main()
{
    solve();
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值