序列變換 2015百度之星(初賽)1005

F - 序列變換
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

我們有一個數列A1,A2...An,你現在要求修改數量最少的元素,使得這個數列嚴格遞增。其中無論是修改前還是修改後,每個元素都必須是整數。 
請輸出最少需要修改多少個元素。

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;
 } 


發佈了93 篇原創文章 · 獲贊 4 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章