Description
請輸出最少需要修改多少個元素。
Input
每一組數據:
第一行輸入一個,表示數列的長度
第二行輸入N個數。
每一個數列中的元素都是正整數而且不超過。
Output
Case #i:
然後輸出最少需要修改多少個元素。
Sample Input
2 2 1 10 3 2 5 4
Sample Output
Case #1: 0 Case #2: 1
LIS,很考思維,把輸入的每一個數都減去它的位數也就是a[i] = a[i]-i;在排一下最大上升子序列,用總數減去序列個數就是要改變的個數
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int M = 1e5+10;
const int inf = 99999999;
int a[M],f[M];
int n,ans;
int main()
{
int text = 1;
int t;cin>>t;
while(t--){
cin>>n;
memset(a,0,sizeof(a));
for(int i=1; i<=n; i++){
scanf("%d",&a[i]);
a[i] = a[i]-i;f[i] = inf;
}
ans =0;
for(int i=1; i<=n; i++){
int k = upper_bound(f+1,f+n+1,a[i])-f;//嚴格
f[k] = min(f[k],a[i]);
ans = max(k,ans);
}
printf("Case #%d:\n%d\n",text++,n-ans);
}
return 0;
}