E - Bellovin
Time Limit:3000MS Memory Limit:131072KB 64bit IO Format:%I64d & %I64u
Submit
Status
Description
Peter has a sequence and he define a function on the sequence – , where is the length of the longest increasing subsequence ending with .
Peter would like to find another sequence in such a manner that equals to . Among all the possible sequences consisting of only positive integers, Peter wants the lexicographically smallest one.
The sequence is lexicographically smaller than sequence , if there is such number from to , that for and .
Input
There are multiple test cases. The first line of input contains an integer , indicating the number of test cases. For each test case:
The first contains an integer – the length of the sequence. The second line contains integers .
Output
For each test case, output integers 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
- 题意:给定序列a1,a2,a2…..aN,f(a i)代表以a i为结尾元素的最大上升子序列长度,让你求出一个序列b1,b2….bN,使任意的f(a i)=f(b i),求出满足要求的最小序列并输出(b i>1,从前向后贪心满足最小的);
- 思路:要输出最小的,那第一个肯定是1,第二个就要看f(a2),如果f(a2)=1那么第二个肯定选1,如果不成立,那么为2,以此类推可以知道bi=f(bi)=f(ai),就是求出每一位的最长上升子序列的长度;
- 失误:用vector写不知道哪错了,指针一直不对,调试好久也找不出来,最后一急把代码调试的一删,没想到就对了,错误还能这样找;
- 代码如下:
#include<cstdio>
#include<vector>
using namespace std;
int main()
{
int T,N,i,tem;
scanf("%d",&T);
while(T--)
{
scanf("%d",&N);
scanf("%d",&tem); vector<int> VEC,ord; vector<int>::iterator iter;
VEC.push_back(tem); ord.push_back(1);
for(i=2;i<=N;++i)
{
scanf("%d",&tem);
iter = lower_bound(VEC.begin(),VEC.end(),tem);
ord.push_back( (iter-VEC.begin())+1);//用队列可能会更方便
if(iter==VEC.end()) VEC.push_back(tem);
else *iter = tem;
}
int k=0;
for(iter=ord.begin();iter!=ord.end();++iter)
{
if(k!=0) printf(" "); k=1;
printf("%d",*iter);
}
printf("\n");
}
return 0;
}