题目描述
分析
一个序列是等差数列且公差不为1的条件就是各项差的绝对值的(gcd!=1)
从前往后依次扫即可
一定要判重
代码
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<map>
inline int read(){
int x=0,fh=1;
char ch=getchar();
while(ch<'0' || ch>'9'){
if(ch=='-') fh=-1;
ch=getchar();
}
while(ch>='0' && ch<='9'){
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
return x*fh;
}
const int maxn=1e6+5;
int gcd(int aa,int bb){
if(bb==0) return aa;
return gcd(bb,aa%bb);
}
int n,a[maxn];
std::map<int,int> vis;
int main(){
freopen("factory.in","r",stdin);
freopen("factory.out","w",stdout);
n=read();
for(int i=1;i<=n;i++){
a[i]=read();
}
int cnt=0,now,cs;
for(int i=1;i<=n;i++){
cnt++;
if(i==n) break;
now=std::abs(a[i]-a[i+1]);
vis[a[i]]=vis[a[i+1]]=cnt;
if(now<=1) continue;
i++;
while(1){
if(vis[a[i+1]]==cnt) break;
vis[a[i+1]]=cnt;
cs=std::abs(a[i]-a[i+1]);
if(cs<=1) break;
now=gcd(now,cs);
if(now<=1) break;
i++;
}
}
printf("%d
",cnt);
return 0;
}