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=,dist2=,l=,r=,ans=;
scanf("%d",&n);
scanf("%d",&a1);
for(int i=;i<=n;i++)
{
scanf("%d",&a);
dist1=a-a1; //根据题意:差值只能为0,1,-1
a1=a; if(dist1==) 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+);
//若子序列的首尾相同(如子序列2 3 3 2),则ans=n-l+1;
printf("%d\n",ans);
return ;
}
上一篇:Responsive设计 (响应式设计)


下一篇:JAVA中I/O流