CF605A Sorting Railway Cars 题解

To CF

这道题依题意可得最终答案为序列长度-最长子序列长度。

数据范围至 \(100000\)\(O(n^2)\) 的复杂度肯定会炸。

\(O(nlogn)\) 的复杂度却在第 \(21\) 个测试点莫名出错。

于是换一种思路可得 \(dp[s]=dp[s-1]+1\) 即类似于求最长子序列长度。

最后用序列长度-最长子序列长度即为答案。

#include<cstdio>
#include<iostream>
using namespace std;
int n;
int s;
int dp[100005];
int ans;
int main(){
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&s);
		dp[s]=dp[s-1]+1;
		ans=max(ans,dp[s]);//最长子序列长度
	}
	printf("%d",n-ans);//序列长度减去最长子序列长度
	return 0;
}
posted @ 2022-07-13 13:26  Scorilon  阅读(43)  评论(0)    收藏  举报