1551: Longest Increasing Subsequence Again

Time Limit: 2 Sec  Memory Limit: 256 MB
Submit: 75  Solved: 52

Description

Give you a numeric sequence. If you can demolish arbitrary amount of numbers, what is the length of the longest increasing sequence, which is made up of consecutive numbers? It sounds like Longest Increasing Subsequence at first sight. So, there is another limitation: the numbers you deleted must be consecutive.

Input

There are several test cases.
For each test case, the first line of input contains the length of sequence N(1≤N≤10^4). The second line contains the elements of sequence——N positive integers not larger than 10^4.

Output

For each the case, output one integer per line, denoting the length of the longest increasing sequence of consecutive numbers, which is achievable by demolishing some(may be zero) consecutive numbers.

Sample Input

7
1 7 3 5 9 4 8
6
2 3 100 4 4 5

Sample Output

4
4

HINT

 

Source

解题:线段树。。

 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
struct node{
int lt,rt,value;
}tree[maxn<<];
struct Block {
int lt,rt;
} block[maxn];
void build(int lt,int rt,int v) {
tree[v].lt = lt;
tree[v].rt = rt;
tree[v].value = ;
if(lt == rt) return;
int mid = (lt + rt)>>;
build(lt,mid,v<<);
build(mid+,rt,v<<|);
}
int query(int id,int v) {
if(tree[v].lt == tree[v].rt) return ;
int mid = (tree[v].lt + tree[v].rt)>>;
if(id <= mid) return max(query(id,v<<),tree[v<<|].value);
return query(id,v<<|);
}
void update(int id,int v,int value) {
int mid = (tree[v].lt + tree[v].rt)>>;
tree[v].value = max(tree[v].value,value);
if(tree[v].lt == tree[v].rt) return;
if(id <= mid) update(id,v<<,value);
else update(id,v<<|,value);
}
int n,m,d[maxn],discrete[maxn],width[maxn];
int main() {
while(~scanf("%d",&n)) {
for(int i = m = ; i < n; ++i) {
scanf("%d",d+i);
discrete[i] = d[i];
}
sort(discrete,discrete+n);
int len = unique(discrete,discrete+n) - discrete;
build(,len-,);
block[m].lt = block[m].rt = ;
for(int i = ; i < n; ++i)
if(d[i-] < d[i]) block[m].rt++;
else {
++m;
block[m].lt = block[m].rt=i;
}
for(int i = ; i <= m; ++i)
for(int j = block[i].rt; j >= block[i].lt; --j)
width[j] = block[i].rt-j+;
int ans = ;
for(int i = m; i >= ; --i) {
for(int j = block[i].rt; j >= block[i].lt; --j) {
int id = lower_bound(discrete,discrete+len,d[j])-discrete;
ans = max(j - block[i].lt + + query(id,),ans);
update(id,,width[j]);
}
}
printf("%d\n",ans);
}
return ;
}

最新文章

  1. python中的时间转换
  2. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q32-Q35)
  3. $_POST 变量以及$GLOBALS[&#39;HTTP_RAW_POST_DATA&#39;]
  4. CSS display 属性
  5. MySQL各逻辑模块工作配合
  6. POJ 3641
  7. Java IO流中的File类学习总结
  8. ANDROID_MARS学习笔记_S01_012_SeekBar
  9. Memcached(四)Memcached的CAS协议
  10. [HeadFirst-HTMLCSS学习笔记][第十二章HTML5标记]
  11. secureCRT使用VIM 像LINUX中那样对语法高亮
  12. EXE转JPG后缀格式工具(真实JPG后缀)
  13. pl/sql developer 连接服务器上的数据库
  14. C# Type.GetType 返回NULL 问题解决记录
  15. Java学习笔记35(sql补充)
  16. 【centos】centos中添加一个新用户,并授权
  17. Postman—使用数据文件
  18. js中push和join方法使用介绍
  19. jQuery-理解事件
  20. Java 学习笔记(1)——java基础语法

热门文章

  1. HDU 5303 Delicious Apples (2015多校第二场 贪心 + 枚举)
  2. zzulioj--1817--match number(水题)
  3. 2.boost遍历数组容器
  4. POJ 2528 线段树
  5. SFTP的使用
  6. 把asp:CheckBoxList 变成单选框
  7. &lt;Three.js&gt;(第一节)环境搭建
  8. gym 100971 J Robots at Warehouse
  9. CentOS 7.4 ifconfig, ip/ss, nmcli, nmtui, 配置文件 修改ip信息用法
  10. 联想服务器thinkserver TS550 Raid5制作及winserver2012R2 安装过来