CF 602B Approximating a Constant Range

(●'◡'●)

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdlib>
#include<vector>
#include<stack>
#include<map>
using namespace std;
typedef long long ll;

int main()
{
    int n,a1,a,dist1=0,dist2=0,l=1,r=1,ans=0;
    scanf("%d",&n);
    scanf("%d",&a1);
    for(int i=2;i<=n;i++)
    {
        scanf("%d",&a);
        dist1=a-a1; //根据题意:差值只能为0,1,-1
        a1=a;

        if(dist1==0) continue; //差值为0的继续往前走
        if(dist1!=dist2) //差值不为0且与前一个差值不相等
        {
            dist2=dist1;  //更新当前区间的差值
            r=i;
        }
        else //差值不为0但与前一个差值相同(如子序列1 2 3)
        {
            ans=max(ans,i-l); //更新区间长度
            l=r;r=i;
        }
    }
    ans=max(ans,n-l+1);
    //若子序列的首尾相同(如子序列2 3 3 2),则ans=n-l+1;
    printf("%d\n",ans);
    return 0;
}
posted @ 2016-02-25 12:07  &ATM  阅读(237)  评论(0编辑  收藏  举报
……