【HDU5748】Bellovin

206人阅读 评论(0)

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

【题解】

if (a[i] > f[len]) //这个很好理解吧？就是如果大于长度为len的最后一个元素.则最长XX的长度递增。

{
len++;

f[len] = a[i]; //然后把新的元素接上去就好

}

else

{

f[k-1]<a[i]<=f[k];

}

【代码】

#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

int t, n, a[100001], f[100001], last = 1, ans[100001];

int main()
{
scanf("%d", &t);
for (int mm = 1; mm <= t; mm++)
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)//读入数据
scanf("%d", &a[i]);
f[1] = a[1];//最长不下降子序列长度为1的最小结尾元素一开始就是a[1]
ans[1] = 1;//没用的。。别管
printf("1");//表示以a1为结尾的最长不下降子序列的长度为1
last = 1;
for (int i = 2; i <= n; i++)
{
if (f[last] < a[i])//因为更新了最长不下降子序列的长度
{
last++;
printf(" %d", last);//所以以其结尾的元素的长度为last;
f[last] = a[i];
}
else
{
int pos = lower_bound(f + 1, f + 1 + last, a[i]) - f;//找到合适的K值
f[pos] = a[i];//放在这个位置
printf(" %d", pos);//输出它的长度、即以a[i]为结尾的最长不下降子序列的长度。
}
}
printf("\n");
}
return 0;
}


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：224922次
• 积分：15134
• 等级：
• 排名：第742名
• 原创：1348篇
• 转载：4篇
• 译文：0篇
• 评论：32条
交流
QQ 190741511
大神博客
文章分类
阅读排行