HDU - 5256 序列变换

我们有一个数列A1,A2...An,你现在要求修改数量最少的元素,使得这个数列严格递增。其中无论是修改前还是修改后,每个元素都必须是整数。 
请输出最少需要修改多少个元素。

Input

第一行输入一个T(1≤T≤10)T(1≤T≤10),表示有多少组数据 

每一组数据: 

第一行输入一个N(1≤N≤105)N(1≤N≤105),表示数列的长度 

第二行输入N个数A1,A2,...,AnA1,A2,...,An。 

每一个数列中的元素都是正整数而且不超过106106。

Output

对于每组数据,先输出一行 

Case #i: 

然后输出最少需要修改多少个元素。

Sample Input

2
2
1 10
3
2 5 4

Sample Output

Case #1:
0
Case #2:
1

题意:把一个数组改成一个递增数组,至少要改多少个数

思路:  如果直接先求出最长上升子序列size  再ans=n-size 这个思路是错误的

比如  1 2 2 2 3 

正确思路是  先将 a[i]-i,然后求出最长不递减上升子序列size ans=n-size   

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstdlib>
#include<deque>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>P;
const int len=1e5+5;
const double pi=acos(-1.0);
const ll mod=1e9+7; 
int a[len],b[len];
int dp[len];
int main()
{	
	int Case=0;
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		scanf("%d",&n);
		for(int i=0;i<n;++i)scanf("%d",&a[i]),a[i]-=i;
		int l=0;
		for(int i=0;i<n;++i)
		{
			int p=upper_bound(b,b+l,a[i])-b;
			if(p==l||b[p]>b[l-1])b[l++]=a[i];
			else b[p]=a[i];
		}
		printf("Case #%d:\n",++Case);
		printf("%d\n",n-l);
	}
}


猜你喜欢

转载自blog.csdn.net/hutwuguangrong/article/details/86584694