如果a[i]和a[j]想不变,需要满足的条件就是
a[j] - a[i] > j - i
也就是a[i] - i < a[j] - j
比如1 4 2 就不满足,所以1和2之间一定有一个需要改变
所以我们对所有a[i] - i求其最长上升子序列就可以了
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 100005; const int INF = 5000000; int n; int arr[maxn],g[maxn]; int main(){ int T,Case = 1; scanf("%d",&T); while(T--){ scanf("%d",&n); for(int i = 1; i <= n; i ++) g[i] = INF; for(int i = 1; i <= n; i++){ scanf("%d",&arr[i]); arr[i] -= i; } int ans = 0; for(int i = 1; i <= n; i++){ int pos = upper_bound(g + 1,g + n + 1,arr[i]) - g; ans = max(ans,pos); g[pos] = arr[i]; } printf("Case #%d:\n",Case++); printf("%d\n",n - ans); } return 0; }
时间: 2024-10-12 11:55:36