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 1≤k<i and ai<bi.
The first contains an integer n (1≤n≤100000) -- the length of the sequence. The second line contains n integers a1,a2,...,an (1≤ai≤109).
#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]);
}
}