Peter has a sequence
a1,a2,...,an
a1,a2,...,an and he define a function on the sequence --
F(a1,a2,...,an)=(f1,f2,...,fn)
F(a1,a2,...,an)=(f1,f2,...,fn), where
fi
fi is the length of the longest increasing subsequence ending with
ai
ai.
Peter would like to find another sequence b1,b2,...,bn b1,b2,...,bn in such a manner that F(a1,a2,...,an) F(a1,a2,...,an) equals to F(b1,b2,...,bn) 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 a1,a2,...,an is lexicographically smaller than sequence b1,b2,...,bn b1,b2,...,bn, if there is such number i i from 1 1 to n n, that ak=bk ak=bk for 1≤k<i 1≤k<i and ai<bi ai<bi.
Peter would like to find another sequence b1,b2,...,bn b1,b2,...,bn in such a manner that F(a1,a2,...,an) F(a1,a2,...,an) equals to F(b1,b2,...,bn) 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 a1,a2,...,an is lexicographically smaller than sequence b1,b2,...,bn b1,b2,...,bn, if there is such number i i from 1 1 to n n, that ak=bk ak=bk for 1≤k<i 1≤k<i and ai<bi ai<bi.
The first contains an integer n n (1≤n≤100000) (1≤n≤100000) -- the length of the sequence. The second line contains n n integers a1,a2,...,an a1,a2,...,an (1≤ai≤109) (1≤ai≤109).
3 1 10 5 5 4 3 2 1 3 1 3 5
1 1 1 1 1 1 1 2 3这道题题意就是给出一个序列,求出以每个元素结尾的最长子序列的长度,然后按序输出就行
#include <cstdio>
#include <algorithm>
#define INF 0x3f3f3f3f
#define maxx 100000+10
using namespace std;
int dp[maxx],a[maxx],t[maxx];
int main()
{
int n,i,j;
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
dp[i]=INF;
}
for(i=0;i<n;i++)
{
*lower_bound(dp,dp+n,a[i])=a[i];
t[i]=lower_bound(dp,dp+n,a[i])-dp+1;//减去后腰加上1才是个数
}
for(int i = 0; i < n-1; i++)
printf("%d ",t[i]);
printf("%d\n",t[n-1]);
}
return 0;
}