Bellovin

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

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
 
题意:求以ai(0<i<n)结尾的最长上升子序列的个数.
题解:在LIS的O(n*log(n))的模板里面找到加一个dp数组记录每一位的最长上升子序列个数,顺序输出即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include <algorithm>
using namespace std;
const int N = ;
int a[N];
int dp[N],c[N]; int main()
{
int tcase;
scanf("%d",&tcase);
while(tcase--)
{
int n;
scanf("%d",&n);
for(int i=; i<=n; i++)
{
scanf("%d",&a[i]);
}
int MAX = ,j=;
c[] = a[];
dp[] = ;
for(int i=; i<=n; i++)
{
if(a[i]<=c[]) j = ;
else if(a[i]>c[MAX]) j = ++MAX;
else j = lower_bound(c+,c++MAX,a[i])-c;
c[j] = a[i];
dp[i] = j;
}
for(int i=; i<n; i++)
{
printf("%d ",dp[i]);
}
printf("%d\n",dp[n]);
}
return ;
}

hdu 5748(LIS)的更多相关文章

  1. hdu 5748(LIS) Bellovin

    hdu 5748 Peter有一个序列a1,a2,...,ana_1,a_2,...,a_na​1​​,a​2​​,...,a​n​​. 定义F(a1,a2,...,an)=(f1,f2,...,fn ...

  2. hdu 5748(求解最长上升子序列的两种O(nlogn)姿势)

    Bellovin Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): Accepte ...

  3. Super Jumping! Jumping! Jumping!(hdu 1087 LIS变形)

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  4. HDU 1025 LIS二分优化

    题目链接: acm.hdu.edu.cn/showproblem.php?pid=1025 Constructing Roads In JGShining's Kingdom Time Limit: ...

  5. HDU 1950(LIS)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1950 Bridging signals Time Limit: 5000/1000 MS (Java ...

  6. hdu 1087(LIS变形)

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  7. hdu 1025 lis 注意细节!!!【dp】

    感觉这道题浪费了我半个小时的生命......哇靠!原来输出里面当len=1时是road否则是roads!!! 其实做过hdu 1950就会发现这俩其实一样,就是求最长上升子序列.我用结构体记录要连线的 ...

  8. HDU 1025 (LIS+二分) Constructing Roads In JGShining's Kingdom

    这是最大上升子序列的变形,可并没有LIS那么简单. 需要用到二分查找来优化. 看了别人的代码,给人一种虽不明但觉厉的赶脚 直接复制粘贴了,嘿嘿 原文链接: http://blog.csdn.net/i ...

  9. hdu 5256 LIS变形

    给一个数列,问最少修改多少个元素使数列严格递增.如果不是要求“严格”递增,那就是求最长不降子序列LIS,然后n-LIS就是答案.要严格递增也好办,输入的时候用每个数减去其下标处理一下就行了. /* * ...

随机推荐

  1. mysql数据库,编码错误解决

    在写代码的过程中,经常会遇见,将中文字符输入到mysql数据库中,但是查看的时候,却发现,中文显示为乱码的情况,让人相当的头疼,今天正好解决了一个这样遇到的问题,所以简单总结一下: 1.首先查看数据库 ...

  2. 下拉框select chosen被遮盖

    最简单的就是让容器高度大点. 用js调整也行. 为什么z-index不管事,看下面... 浏览器支持 所有主流浏览器都支持 z-index 属性. 注释:任何的版本的 Internet Explore ...

  3. [剑指Offer] 23.二叉搜索树的后序遍历

    [思路]BST的后序序列的合法序列是,对于一个序列S,最后一个元素是x (也就是根),如果去掉最后一个元素的序列为T,那么T满足:T可以分成两段,前一段(左子树)小于x,后一段(右子树)大于x,且这两 ...

  4. 【bzoj1260】[CQOI2007]涂色paint 区间dp

    题目描述 给出一个序列,每次可以给一段染成同一种颜色,问最少要染多少次能够染成给定方案. 输入 输入仅一行,包含一个长度为n的字符串,即涂色目标.字符串中的每个字符都是一个大写字母,不同的字母代表不同 ...

  5. 一道js的前端面试题,主要弄清楚逗号表达式的作用

    群里看到的一道前端JS面试题.我以为我会,其实我错了.找了很多资料,写下来. var i,j,k; for( i = 0 , j = 0; i < 10 , j < 6; i++ , j+ ...

  6. ASP.NET页面之间传值Cookie(3)

    这个也是大家常使用的方法,Cookie用于在用户浏览器上存储小块的信息,保存用户的相关信息,比如用户访问某网站时用户的ID,用户的偏好等, 用户下次访问就可以通过检索获得以前的信息.所以Cookie也 ...

  7. P2127 序列排序

    题目描述 小C有一个N个数的整数序列,这个序列的中的数两两不同.小C每次可以交换序列中的任意两个数,代价为这两个数之和.小C希望将整个序列升序排序,问小C需要的最小代价是多少? 输入输出格式 输入格式 ...

  8. [codeforces] 17E Palisection

    原题 题目要求相交的回文串对数,这显然非常难,但是要有一种正难则反的心态,求不出来相交的,求出来不相交的不就好了! 对于每以位置i结尾的字符串,在他后面与他不相交的就是以这个位置为结尾的个数和以这个位 ...

  9. 网络编程:listen函数

    listen函数仅由TCP服务器调用,它做两件事: 当socket函数创建一个套接字时,它被假设为一个主动套接字,也就是说,它是一个将调用connect发起连接的客户套接字.listen函数把一个未连 ...

  10. C++——类继承以及类初始化顺序

    对于类以及类继承, 几个主要的问题:1) 继承方式: public/protected/private继承. 这是c++搞的, 实际上继承方式是一种允许子类控制的思想. 子类通过public继承, 可 ...