题目

Solution

求d=1的最长等差子序列

Code

#include<bits/stdc++.h>
using namespace std;
const int N=50002;
int n,i,ans,f[N],a[N],x;
inline char gc(){
	static char buf[100000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int rd(){
	int x=0,fl=1;char ch=gc();
	for (;ch<48||ch>57;ch=gc())if(ch=='-')fl=-1;
	for (;48<=ch&&ch<=57;ch=gc())x=(x<<3)+(x<<1)+(ch^48);
	return x*fl;
}
int main(){
	n=rd();
	for (i=1;i<=n;i++) x=rd(),f[x]=f[x-1]+1,ans=max(ans,f[x]);
	printf("%d",n-ans);
}