題意:一個數列,a1,a2,a3,a4,…,an,需要最少修改多少個元素,使得這個序列嚴格遞增?
a[i]-i 非遞減序列 因爲a[i] < a[i+1],且a[i],a[i+1]都是整數,所以有a[i+1]-1>=a[i],整理得a[i+1]-(i+1)>=a[i]-i。令b[i]=a[i]-i。則可以求出b[i]的最長不下降子序列的長度len,最後用n-len即爲需要改變的最少的元素個數。
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
using namespace std;
int b[100010];
int a[100010];
int dp[100010];
int main(void)
{
int t;
scanf("%d",&t);
int n;
int cs = 1;
while(t--){
scanf("%d",&n);
for(int i = 0; i < n; i++){
scanf("%d",&a[i]);
a[i] = a[i] - i;
}
int len = 1;
b[0] = a[0];
for(int i = 1; i < n; i++){
if(a[i] >= b[len-1]) b[len++] = a[i];
int pos = upper_bound(b,b+len,a[i]) - b;
b[pos] = a[i];
}
printf("Case #%d:\n%d\n",cs++,n-len);
}
return 0;
}