通过找规律,发现其实就是求最长公差为1的子序列,显示需要dp,不然O(n*n)会TLE。
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=1e5+5;
int dp[maxn];
int main()
{
int n;
while(scanf("%d",&n)==1)
{
int ans=0,x;
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
dp[x]=max(dp[x],dp[x-1]+1);
ans=max(ans,dp[x]);
}
printf("%d\n",n-ans);
}
return 0;
}