Another LIS

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1244    Accepted Submission(s): 431

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

 
题意:一个空序列,第k次在xk处 插入 k,并输出 序列当前的LIS。
首先 倒序 用线段树来确定每个元素 最终的位置。然后 求n次LIS,当然要用线段树维护了,,或者二分等等也可以。 nlogn
 #include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1e5+;
int seg[maxn<<],a[maxn]; // seg表示该区间 位置剩余量
void build (int l,int r,int pos)
{
if (l == r)
{
seg[pos] = ;
return;
}
int mid = (l + r) >> ;
build(l,mid,pos<<);
build(mid+,r,pos<<|);
seg[pos] = seg[pos<<] + seg[pos<<|];
}
int ans[maxn]; //ans[i]表示 元素i的位置为ans[i]
void Insert(int l,int r,int pos,int x,int k)
{
if (l == r)
{
ans[k] = l;
seg[pos] = ;
return ;
}
int mid = (l + r) >> ;
if (seg[pos<<] >= x) //左孩子的区间 位置剩余量大于x时,递归进入左孩子,否则右孩子同时 x-seg[pos<<1]
Insert(l,mid,pos<<,x,k);
else
Insert(mid+,r,pos<<|,x - seg[pos<<],k);
seg[pos] = seg[pos<<] + seg[pos<<|];
}
void update(int l,int r,int pos,int x,int v)
{
if (l == r)
{
seg[pos] = v;
return;
}
int mid = (l + r) >> ;
if (x <= mid)
update(l,mid,pos<<,x,v);
else
update(mid+,r,pos<<|,x,v);
seg[pos] = max(seg[pos<<],seg[pos<<|]);
}
int query(int l,int r,int pos,int ua,int ub)
{
if (ua <= l && ub >= r)
{
return seg[pos];
}
int mid = (l + r) >> ;
int t1 = ,t2 = ;
if (ua <= mid)
t1 = query(l,mid,pos<<,ua,ub);
if (ub > mid)
t2 = query(mid+,r,pos<<|,ua,ub);
return max(t1,t2);
} int main(void)
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int t,cas = ;
scanf ("%d",&t);
while (t--)
{
int n;
scanf ("%d",&n);
for (int i = ; i <= n; i++)
scanf ("%d",a+i);
build (,n,);
for (int i = n; i >= ; i--)
Insert(,n,,a[i] + ,i); //倒序 确定每个元素 最终的位置保存在ans里
memset(seg,,sizeof(seg));
printf("Case #%d:\n",cas++);
for (int i = ; i <= n; i++)
{
int t1 = query(,n,,,n);
int t2 = query(,n,,,ans[i]);
update(,n,,ans[i],t2+);
if (t2 < t1) //输出当前区间的LIS
printf("%d\n",t1);
else
printf("%d\n",+t2);
}
printf("\n");
}
return ;
}

HDU3564 --- Another LIS (线段树维护最值问题)的更多相关文章

  1. MemSQL Start[c]UP 2.0 - Round 1 F - Permutation 思维+线段树维护hash值

    F - Permutation 思路:对于当前的值x, 只需要知道x + k, x - k这两个值是否出现在其左右两侧,又因为每个值只有一个, 所以可以转换成,x+k, x-k在到x所在位置的时候是否 ...

  2. BZOJ 2124 线段树维护hash值

    思路: http://blog.csdn.net/wzq_QwQ/article/details/47152909 (代码也是抄的他的) 自己写得垃圾线段树怎么都过不了 隔了两个月 再写 再挂 又隔了 ...

  3. cf213E 线段树维护hash

    链接 https://codeforces.com/contest/213/problem/E 题目大意 给出两个排列a.b,长度分别为n.m,你需要计算有多少个x,使 得\(a_1 + x; a_2 ...

  4. FJUT3568 中二病也要敲代码(线段树维护区间连续最值)题解

    题意:有一个环,有1~N编号,m次操作,将a位置的值改为b,问你这个环当前最小连续和多少(不能全取也不能不取) 思路:用线段树维护一个区间最值连续和.我们设出两个变量Lmin,Rmin,Mmin表示区 ...

  5. BZOJ.1036 [ZJOI2008]树的统计Count ( 点权树链剖分 线段树维护和与最值)

    BZOJ.1036 [ZJOI2008]树的统计Count (树链剖分 线段树维护和与最值) 题意分析 (题目图片来自于 这里) 第一道树链剖分的题目,谈一下自己的理解. 树链剖分能解决的问题是,题目 ...

  6. 【uoj#164】[清华集训2015]V 线段树维护历史最值

    题目描述 给你一个长度为 $n$ 的序列,支持五种操作: $1\ l\ r\ x$ :将 $[l,r]$ 内的数加上 $x$ :$2\ l\ r\ x$ :将 $[l,r]$ 内的数减去 $x$ ,并 ...

  7. 【bzoj3064】Tyvj 1518 CPU监控 线段树维护历史最值

    题目描述 给你一个序列,支持4种操作:1.查询区间最大值:2.查询区间历史最大值:3.区间加:4.区间赋值. 输入 第一行一个正整数T,表示Bob需要监视CPU的总时间. 然后第二行给出T个数表示在你 ...

  8. 滑动窗口(poj,线段树维护区间最值)

    题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗口中的最大值和最小值. 例如: The array i ...

  9. CF213E Two Permutations 线段树维护哈希值

    当初竟然看成子串了$qwq$,不过老师的$ppt$也错了$qwq$ 由于子序列一定是的排列,所以考虑插入$1$到$m$到$n-m+1$到$n$; 如何判断呢?可以用哈希$qwq$: 我们用线段树维护哈 ...

随机推荐

  1. Shiro-授权

    把 realms 配置给SecurityManager 在认证的时候单个realm是这样配置的: <bean id="securityManager" class=" ...

  2. github atom创建自己的语法高亮

    使用atom一段时间了,有些插件还不是很成熟.比如项目中使用protobuf,早就有人写了语法高亮(https://github.com/podgib/atom-protobuf),但是效果不是很好. ...

  3. spring整合springMVC、mybatis、hibernate、mongodb框架

    开发环境 eclipse Mars 4.5 JDK 1.7 框架 spring 4.0.5 mybatis 3.2.7 hibernate 4.3.6 mongodb 1.7 数据库 MySQL 5. ...

  4. mysql分表方法-----MRG_MyISAM引擎分表法

    一般来说,当我们的数据库的数据超过了100w记录的时候就应该考虑分表或者分区了,这次我来具体说说分表的一些方法.眼下我所知道的方法都是MYISAM的,INNODB怎样做分表而且保留事务和外键,我还不是 ...

  5. 怎么给iOS项目打包

    1  首先要选中项目中的真机測试,不要模拟器 ,然后从上边的菜单条中找product watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc29tZXJhaW43 ...

  6. gcc -L -l的使用

    -l参数就是用来指定程序要链接的库,-l参数紧接着就是库名,那么库名跟真正的库文件名有什么关系呢?就拿数学库来说,他的库名是m,他的库文件名是libm.so,很容易看出,把库文件名的头lib和尾.so ...

  7. Universal-Image-Loader 示例 工具

    Base public class MyApplication extends Application {     @Override     public void onCreate() {     ...

  8. 【nodejs学习】0.nodejs学习第一天

    1.模块 大一点的程序都需要模块化,nodejs也不例外,代码放到不同的文件中,每一个文件就可以是一个模块,文件路径名就是一个模块名.每个模块中包含三个预先定义的变量: 1.require:用于在当前 ...

  9. MySql不支持主外键

    创建表不支持主外键,能够添加外键成功,但是无法外键约束.查资料发现MySql的默认ENGINE 为MyISAM  ,不支持外键,需要修改为 INNODB 修改前: Create Table CREAT ...

  10. 关于R文件丢失的一个问题

    android studio在编辑布局文件时,一般为了省事,如TextView控件中的text属性这样写 android:text="<500",编译不会报错,但是运行时会出 ...