Problem Description
There is a sequence firstly empty. We begin to add number from 1 to N to the sequence, and every time we just add a single number to the sequence at a specific position. Now, we want to know length of the LIS (Longest Increasing Subsequence)
after every time's add.
 

Input
An integer T (T <= 10), indicating there are T test cases.

For every test case, an integer N (1 <= N <= 100000) comes first, then there are N numbers, the k-th number Xk means that we add number k at position Xk (0 <= Xk <= k-1).See hint for more details.
 

Output
For the k-th test case, first output "Case #k:" in a separate line, then followed N lines indicating the answer. Output a blank line after every test case.
 

Sample Input

1
3
0 0 2
 

Sample Output

Case #1:
1
1
2

Hint

In the sample, we add three numbers to the sequence, and form three sequences.
a. 1
b. 2 1
c. 2 1 3

这题看了别人的题解,最后看懂了。题意是从1~n,一次插入位置,然后每插入一个数求出它的最长递增子序列(不连续)。可以用线段树先求出每个数所在的位置,可以从后往前,因为每次最后一个数的位置一定是固定的。然后就是二分法的lis了,这里如果继续用线段树求lis的话会超时的。
这里二分的lis之前一直看不懂,其实因为输入的数是一次增大的,所以每次只要记录这个数的位置,然后二分找到最接近但大于当前数的位置,然后替换掉找到的数。
这题我对二分又有了新的认识,二分模板不是固定的,而是根据题目意思决定最后返回的值是l还是r.
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define maxn 100005
int a[maxn],ans[maxn],dp[maxn];
struct node{
int l,r,n;
}b[4*maxn]; void build(int l,int r,int i)
{
int mid;
b[i].l=l;b[i].r=r;b[i].n=r-l+1;
if(l==r)return;
mid=(l+r)/2;
build(l,mid,i*2);
build(mid+1,r,i*2+1);
} void update(int index,int m,int i)
{
int mid;
if(b[i].l==b[i].r){
b[i].n=0;ans[m]=b[i].l;return;
}
if(b[i*2].n>=index) update(index,m,i*2);
else update(index-b[i*2].n,m,i*2+1);
b[i].n=b[i*2].n+b[i*2+1].n;
} int find(int l,int r,int x)
{
int mid;
while(l<=r){
mid=(l+r)/2;
if(dp[mid]>x){
r=mid-1;
}
else l=mid+1;
}
return r+1; //这里也可以是l,可以草稿纸上画一下
} int main()
{
int n,m,i,j,h,T,k,len; //len表示最长递增子序列
scanf("%d",&T);
for(h=1;h<=T;h++)
{
printf("Case #%d:\n",h);
scanf("%d",&n);
for(i=1;i<=n;i++){scanf("%d",&a[i]);dp[i]=0;}
build(1,n,1);
for(i=n;i>=1;i--){
update(a[i]+1,i,1);
}
len=0;
for(i=1;i<=n;i++){
if(len==0){
dp[++len]=ans[1]; //ans[]表示i所在的位置
printf("%d\n",len);
continue;
}
k=find(1,len,ans[i]);
len=max(len,k); //不管这n个数排列顺序怎样,每一次的最长递增子序列一定是递增的,可以画一下。
dp[k]=ans[i]; //dp[k]表示最长递增序列中的第k个数,用到了单调队列的思想
printf("%d\n",len);
}
printf("\n");
}
return 0;
}

hdu3564 Another LIS的更多相关文章

  1. HDU3564 --- Another LIS (线段树维护最值问题)

    Another LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  2. BZOJ3173:[TJOI2013]最长上升子序列 & HDU3564:Another LIS——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=3173 http://acm.hdu.edu.cn/showproblem.php?pid=3564 ...

  3. Lis日常维护

    1.[问题]护士站打印LIs条码,出来是PDF格式的 [解决]在文件夹Client\NeusoftLis\Xml\Print.xml中把BarcodePrint Name的值改成安装的斑马打印机名(不 ...

  4. uva10635 LIS

    Prince and PrincessInput: Standard Input Output: Standard Output Time Limit: 3 Seconds In an n x n c ...

  5. Codeforces 486E LIS of Sequence 题解

    题目大意: 一个序列,问其中每一个元素是否为所有最长上升子序列中的元素或是几个但不是所有最长上升子序列中的元素或一个最长上升子序列都不是. 思路: 求以每一个元素为开头和结尾的最长上升子序列长度,若两 ...

  6. 出操队形(LIS)

    题目来源:微策略2013年校园招聘面试一面试题 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往楼下跑了,然后身高矮的排在队伍的前面,身高较 ...

  7. 洛谷P1108 低价购买[DP | LIS方案数]

    题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它 ...

  8. [tem]Longest Increasing Subsequence(LIS)

    Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...

  9. 从LIS问题浅谈动态规划

    今天以LIS问题切入动态规划,现在做一些简单的总结. LIS问题: http://www.cnblogs.com/Booble/archive/2010/11/27/1889482.html

随机推荐

  1. 【SpringBoot1.x】SpringBoot1.x 入门

    SpringBoot1.x 入门 文章源码 简介 传统的 JavaEE 开发,十分笨重且配置繁琐,开发效率很低,而且有很复杂的部署流程,对于第三方技术的集成也很困难. Sring 全家桶时代则解决了上 ...

  2. python模块/文件/日期时间

    文件操作:

  3. SpringBoot 导入插件报错 Cannot resolve plugin org.springframework.boot:spring-boot-maven-plugin:2.4.1

    使用 maven 导入插件的时候报错: Cannot resolve plugin org.springframework.boot:spring-boot-maven-plugin:2.4.1 我的 ...

  4. 关于QTableWidget中单元格拖拽实现

    无重写函数实现单元格拖拽 缺点:需要额外设置一个记录拖拽起始行的私有成员变量和拖拽列的初始QList数据成员. 优点:无需重构函数,对于QT中信号和槽的灵活运用 信号和槽 // signal void ...

  5. 【MySQL】CentOS7中使用systemctl工具管理启动和停止MySQL

    centos7以前版本,可以使用这个/etc/init.d/mysqld start 来启动mysql 但是centos7之后,通过systemctl start mysqld.service 这个要 ...

  6. kubernets之configMap和secret

    一  如何有效且更好的将配置写到pod的容器中 考虑一个问题,就是在传统的应用中,程序里面需要的配置一般以配置文件的形式或者shell脚本里面的参数是在执行的时候在命令行里面进行添加,但是在kuber ...

  7. python—打开图像文件报错

    今天使用python打开一张图像文件的时候报错了 UnicodeDecodeError: 'gbk' codec can't decode byte 0xff in position 0: illeg ...

  8. oracle rac与单实例DG切换

    1.主库查看状态(RAC库) SQL> select database_role,switchover_status from v$database; DATABASE_ROLE SWITCHO ...

  9. Java-web易混淆知识点整理

    Java-web易混淆知识点 post和get区别 post: 数据不会显示在地址栏 安全 大小无限制 可以提交二进制文件 get: 数据显示在地址栏 不安全 get方式提交有大小限制(约4kb) 相 ...

  10. Django-初阶实例

    调用本地css文件的方法 setting.py里面的内容 import os # Build paths inside the project like this: os.path.join(BASE ...