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

【题解】

这题就是要求最长不下降子序列。

不同的是,它不是要求你求出来最长不下降子序列的各个值具体是什么。

它要求的是以ai结尾的最长不下降子序列的长度f[i].

但是这题的坑点在于。它不让你用那么方便的n^2算法。要用nlogn算法才行。所以就去找咯。

原来的f[i]要改一下意思了。变成长度为i的最长上升(严格上升)序列的最后一个元素的最小值是啥。

一开始len = 1;f[1] = a1;

然后for i = 2->n

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

{

len++;

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

直接输出len.表示以a[i]结尾的最长不下降子序列的长度为len;

}

else

{

找到一个k

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

因为a[i] <= f[k]且f[k-1]<a[i] 则说明其可以代替f[k]成为一个更小的f[k]'。

那么就让f[k] = a[i]就好;

这个k可以用二分查找找到(鬼才想去写这个二分查找。跟屎一样难写!)

所以我们用STL解决(就是algorithm这个头文件里的东西。)

它叫。我看下我能不能默下来lower__count??

我看下。

哦,错了

是lower_bound(f+1,f+1+len,a[i])-f;

你没看错最后面减去了一个数组

然后前面就是类似sort(a+1,a+1+len);

表示从1..len找到这样的k;

然后f[k] = a[i];

然后输出k.表示以a[i]为结尾的最长不下降子序列的长度为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;
}

【HDU5748】Bellovin的更多相关文章

  1. Python高手之路【六】python基础之字符串格式化

    Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...

  2. 【原】谈谈对Objective-C中代理模式的误解

    [原]谈谈对Objective-C中代理模式的误解 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 这篇文章主要是对代理模式和委托模式进行了对比,个人认为Objective ...

  3. 【原】FMDB源码阅读(三)

    [原]FMDB源码阅读(三) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 FMDB比较优秀的地方就在于对多线程的处理.所以这一篇主要是研究FMDB的多线程处理的实现.而 ...

  4. 【原】Android热更新开源项目Tinker源码解析系列之一:Dex热更新

    [原]Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Tinker是微信的第一个开源项目,主要用于安卓应用bug的热修复和功能的迭代. Tinker github地址:http ...

  5. 【调侃】IOC前世今生

    前些天,参与了公司内部小组的一次技术交流,主要是针对<IOC与AOP>,本着学而时习之的态度及积极分享的精神,我就结合一个小故事来初浅地剖析一下我眼中的“IOC前世今生”,以方便初学者能更 ...

  6. Python高手之路【三】python基础之函数

    基本数据类型补充: set 是一个无序且不重复的元素集合 class set(object): """ set() -> new empty set object ...

  7. Python高手之路【一】初识python

    Python简介 1:Python的创始人 Python (英国发音:/ˈpaɪθən/ 美国发音:/ˈpaɪθɑːn/), 是一种解释型.面向对象.动态数据类型的高级程序设计语言,由荷兰人Guido ...

  8. 【开源】简单4步搞定QQ登录,无需什么代码功底【无语言界限】

    说17号发超简单的教程就17号,qq核审通过后就封装了这个,现在放出来~~ 这个是我封装的一个开源项目:https://github.com/dunitian/LoTQQLogin ————————— ...

  9. 【原】FMDB源码阅读(二)

    [原]FMDB源码阅读(二) 本文转载请注明出处 -- polobymulberry-博客园 1. 前言 上一篇只是简单地过了一下FMDB一个简单例子的基本流程,并没有涉及到FMDB的所有方方面面,比 ...

随机推荐

  1. amazeui学习笔记二(进阶开发3)--HTML/CSS规范Rules

    amazeui学习笔记二(进阶开发3)--HTML/CSS规范Rules 一.总结 1.am:以 am 为命名空间 2.模块状态: {命名空间}-{模块名}-{状态描述} 3.子模块: {命名空间}- ...

  2. HTML基础第七讲---框架

    转自:https://i.cnblogs.com/posts?categoryid=1121494 框架(Frame)也就是所谓的分割窗口.分割画面.框窗效果(还真是五花八门),这个技巧在运用上问题比 ...

  3. AndroidStudio MAT LeakCanary 内存分析之 LeakCanary

    现在我们换一种更清晰方便的方式:LeakCanary https://github.com/square/leakcanary 首先将LeakCanary绑在我们的app上 build.gradle ...

  4. 11G、12C Data Guard Physical Standby Switchover转换参考手册

    Switchover转换   Step 1: switchover 切换先前检查 (1)确保主备两端log_archive_config和db_unique_name参数都已经正确设置. 需要注意的是 ...

  5. php网页跳转无法获取session值

    今日编写项目,需要在跳转后的页面获取session值进行自动登录操作,但是明明在传输页面可以打印出session值,但在接受页面却显示session值为空,经确认脚本中的session_start() ...

  6. 修改shm,oracle11g需要扩大共享内存

    作者:david_zhang@sh [转载时请以超链接形式标明文章] 链接:http://www.cnblogs.com/david-zhang-index/archive/2012/04/26/24 ...

  7. 【CS Round #46 (Div. 1.5) A】Letters Deque

    [链接]h在这里写链接 [题意] 在这里写题意 [题解] string类模拟 [错的次数] 0 [反思] 在这了写反思 [代码] /* */ #include <cstdio> #incl ...

  8. MyBatis学习总结(14)——Mybatis使用技巧总结

    1. 区分 #{} 和 ${}的不同应用场景 1)#{} 会生成预编译SQL,会正确的处理数据的类型,而${}仅仅是文本替换. 对于SQL: select * from student where x ...

  9. Git 经常使用命令

    Git经常使用命令备忘: Git配置 git config --global user.name "storm" git config --global user.email &q ...

  10. Qt产生随机数(两种方法)

    第一种方法 #include <QTime> QTime time; time= QTime::currentTime(); qsrand(time.msec()+time.second( ...