Bellovin

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 858    Accepted Submission(s): 395

Problem Description
Peter has a sequence a1,a2,...,an and he define a function on the sequence -- F(a1,a2,...,an)=(f1,f2,...,fn), where fi is the length of the longest increasing subsequence ending with ai.

Peter would like to find another sequence b1,b2,...,bn in such a manner that F(a1,a2,...,an) equals to 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 is lexicographically smaller than sequence b1,b2,...,bn, if there is such number i from 1 to n, that ak=bk for 1≤k<i and ai<bi.

 
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first contains an integer n (1≤n≤100000) -- the length of the sequence. The second line contains n integers a1,a2,...,an (1≤ai≤109).

 
Output
For each test case, output n integers b1,b2,...,bn (1≤bi≤109) 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
 
Source
 
题意:给一个序列,求出每个位置结尾的最长上升子序列;然后找一个字典序最小的这个函数值相同的子序列;
其实就是求每个位置结尾的最长上升子序列的长度组成的序列。
 
题解:固定尾部的最长上升子序列的长度nlgn处理
exm[j] 表示长度为j的的序列的最后一个值的大小
lower_bound的使用 返回第一个大于等于key的位置 确保严格的上升
这是为什么呢? 找到相同的值结尾的序列 长度却没有变 保证了严格上升
不同于最长不下降(使用upper_bound 跳过了相同的值 也就是长度增加了)。
理解
 
 /******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<algorithm>
#include<cmath>
#define ll __int64
#define PI acos(-1.0)
#define mod 1000000007
using namespace std;
int t;
int a[];
int ans[];
int n;
int main()
{
scanf("%d",&t);
{
for(int i=; i<=t; i++)
{
scanf("%d",&n);
scanf("%d",&a[]);
ans[]=;
int exm[];
int top=;
exm[]=a[];
for(int j=; j<n; j++)
{
scanf("%d",&a[j]); if(a[j]>exm[top])
{
top=top+;
exm[top]=a[j];
ans[j]=top;
}
else
{
int pos=lower_bound(exm,exm+top,a[j])-exm;
exm[pos]=a[j];
ans[j]=pos;
}
}
for(int j=; j<n; j++)
{
if(j==)
printf("%d",ans[j]);
else
printf(" %d",ans[j]);
}
printf("\n");
}
}
return ;
}

HDU 5748 最长上升子序列的长度nlogn(固定尾部)的更多相关文章

  1. hdu 1950 最长上升子序列(lis) nlogn算法【dp】

    这个博客说的已经很好了.http://blog.csdn.net/shuangde800/article/details/7474903 简单记录一下自己学的: 问题就是求一个数列最长上升子序列的长度 ...

  2. 【最长下降子序列的长度和个数】 poj 1952

    转自http://blog.csdn.net/zhang360896270/article/details/6701589 这题要求最长下降子序列的长度和个数,我们可以增加数组maxlen[size] ...

  3. HDU 1025(最长上升子序列)

    题意是要在两条平行线间连点,要在线不交叉的前提下尽可能多的连线,问最多能连多少条线. 现假定题中所给的是 9 组点,分别是:1—3,2—8,3—5,4—9,5—2,6—4,7—6,8—7,9—1,如图 ...

  4. HDU 1159 最长公共子序列(n*m)

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  5. HDU-1257 最少拦截系统 贪心/DP 最长上升子序列的长度==最长不上升子序列的个数?

    题目链接:https://cn.vjudge.net/problem/HDU-1257 题意 中文题咯中文题咯 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然 ...

  6. 最长上升子序列(LIS)长度的O(nlogn)算法

    最长上升子序列(LIS)的典型变形,熟悉的n^2的动归会超时.LIS问题可以优化为nlogn的算法.定义d[k]:长度为k的上升子序列的最末元素,若有多个长度为k的上升子序列,则记录最小的那个最末元素 ...

  7. HDU 5773 最长上升子序列

    题意 给出一个序列 问它的最长严格上升子序列多长 这个序列中的0可以被替代为任何数 n的范围给出了1e5 所以平常的O(n*n)lis不能用了 在kuangbin的模板里有O(nlogn)的模板 套上 ...

  8. HDU 1159 最长公共子序列

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. 最长上升子序列(LIS)长度及其数量

    例题51Nod-1376,一个经典问题,给出一个序列问该序列的LIS以及LIS的数量. 这里我学习了两种解法,思路和代码都是参考这两位大佬的: https://www.cnblogs.com/reve ...

随机推荐

  1. Zabbix页面遇到历史记录的乱码需要修改数据库

    Zabbix页面遇到历史记录的乱码需要修改数据库: 解决办法: 1.将 zabbix 数据库中的表备份: 2.手动删除 zabbix 数据库: 3.重新创建 zabbix 库时手动指定字符集为 utf ...

  2. Python中subprocess学习

    subprocess的目的就是启动一个新的进程并且与之通信. subprocess模块中只定义了一个类: Popen.可以使用Popen来创建进程,并与进程进行复杂的交互.它的构造函数如下: subp ...

  3. CodeForces 546B-Soldier and Badges

    题意: 给出一些数字,要求每个数字都不一样需要增加几 思路: 先排序,然后一个个增加,最后求总和差 代码如下: #include <iostream> #include <cstdi ...

  4. AbstractMap学习记录

    package java.util;import java.util.Map.Entry; /** * This class provides a skeletal implementation of ...

  5. 图解傅里叶变换(so easy)

    话不多说先上两个GIF图. 第一个动画和第二个动画其实都是对时域的周期矩形形波(近似看成矩形波,并不是严格意义的矩形方波)进行傅里叶变换分析. 对于第一个图形来说,它侧重展示变换的本质之一:叠加性,每 ...

  6. asp.net 代码 注意点

    1. 模糊查询时,注意要去掉空格 前台: <input id="txtQJBH" type="text" runat="server" ...

  7. springMVC文件上传(转)

    原文链接: http://www.cnblogs.com/lonecloud/p/5989905.html 在Spring-mvc.xml注入bean 1 <!-- 配置文件上传,如果没有使用文 ...

  8. Register-SPWorkflowService 远程服务器返回错误: (404) 未找到

    博客地址:http://blog.csdn.net/foxdave 当想创建一个SharePoint 2013 工作流的时候,打开SharePoint 2013 Designer(一下简称SPD),发 ...

  9. PHP time() 函数

    定义和用法 time() 函数返回当前时间的 Unix 时间戳. 语法 time(void) 参数 描述 void 可选. 说明 返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 ...

  10. hdu 2048

    PS:WA了两次...主要是没注意到fac的大小好像只能写到9...要用long long型递归求阶乘... 然后就是错排公式...百度下.. 代码: #include "stdio.h&q ...