Peter has a sequence a1,a2,...,ana1,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 fifi is
the length of the longest increasing subsequence ending with aiai.
Peter would like to find another sequence b1,b2,...,bnb1,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,...,ana1,a2,...,an is lexicographically smaller than sequence b1,b2,...,bnb1,b2,...,bn, if there is such number ii from 11 to nn, that ak=bkak=bk for 1≤k<i1≤k<i and ai<biai<bi.
Peter would like to find another sequence b1,b2,...,bnb1,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,...,ana1,a2,...,an is lexicographically smaller than sequence b1,b2,...,bnb1,b2,...,bn, if there is such number ii from 11 to nn, that ak=bkak=bk for 1≤k<i1≤k<i and ai<biai<bi.
The first contains an integer nn (1≤n≤100000)(1≤n≤100000) -- the length of the sequence. The second line contains nn integers a1,a2,...,ana1,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
题意:寻找和一个序列增减性相同的另一个序列,若有多个,按字典序排序最小的输出。。。
思路:输出以第i个数为终点的最长子序列的长度即可;注意,不能用LIS中复杂度为O(n^2)的算法,否则超时。。。
下面附上我的代码:
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
const int M=100010;
int dp[M],a[M],b[M];
int main()
{
int n,p;
scanf("%d",&n);
while(n--)
{
scanf("%d%d",&p,&a[0]);
int ans=1;
dp[1]=a[0];
b[0]=ans;
for(int i=1;i<p;i++)
{
scanf("%d",&a[i]);
if(a[i]>dp[ans])
{
ans++;
dp[ans]=a[i];
b[i]=ans;
}
else
{
b[i]=lower_bound(dp,dp+ans,a[i])-dp;
dp[b[i]]=a[i];
}
}
for(int i=0;i<p;i++)
printf("%d%c",b[i],i==p-1?'\n':' ');
}
return 0;
}