hdoj5748 Bellovin 【BestCoder Round #84】 (最長上升序列)

Problem Description
Peter has a sequence a1,a2,...,an and he define a function on the sequence -- F(a1,a2,...,an)=(f1,f2,...,fn), where fi is the length of the longest increasing subsequence ending with ai.

Peter would like to find another sequence b1,b2,...,bn in such a manner that F(a1,a2,...,an) equals to F(b1,b2,...,bn). Among all the possible sequences consisting of only positive integers, Peter wants the lexicographically smallest one.

The sequence a1,a2,...,an is lexicographically smaller than sequence b1,b2,...,bn, if there is such number i from 1 to n, that ak=bk for 1k<i and ai<bi.
 

Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first contains an integer n (1n100000) -- the length of the sequence. The second line contains n integers a1,a2,...,an (1ai109).
 

Output
For each test case, output n integers b1,b2,...,bn (1bi109) denoting the lexicographically smallest sequence.
 

Sample Input
3 1 10 5 5 4 3 2 1 3 1 3 5
 

Sample Output
1 1 1 1 1 1 1 2 3
 
給你一個含有n個數的序列,a1,a2,a3.......,an.f[ i ]表示以a(i)結尾的序列的最長上升子序列的長度,依次輸出.
代碼如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
int dp[100100];
int a[100100];
int f[100100];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n;
		scanf("%d",&n);
		int i ;
		for(i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
		}
		fill(dp,dp+n,INF);//注意這裏初始化不能用memset 
		for(i=0;i<n;i++)
		{
			*lower_bound(dp,dp+n,a[i])=a[i];//將a[i]插入到第一個大於等於a[i]的位置 
			f[i]=lower_bound(dp,dp+n,a[i])-dp+1;//因爲數組是從0開始的,所以結果要+1 
		}
		for(i=0;i<n-1;i++)
		printf("%d ",f[i]);
		printf("%d\n",f[n-1]);
	}
 } 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章