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

来源:互联网 发布:python 矩阵运算 编辑:程序博客网 时间:2024/05/04 22:04
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
311055 4 3 2 131 3 5
 

Sample Output
11 1 1 1 11 2 3
 
给你一个含有n个数的序列,a1,a2,a3.......,an.f[ i ]表示以a(i)结尾的序列的最长上升子序列的长度,依次输出.
代码如下:
#include<cstdio>#include<cstring>#include<algorithm>#define INF 0x3f3f3f3fusing 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]);} } 


0 0
原创粉丝点击