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 ;
}

CSUOJ 1551 Longest Increasing Subsequence Again的更多相关文章

  1. CSU - 1551 Longest Increasing Subsequence Again —— 线段树/树状数组 + 前缀和&后缀和

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1551 题意: 给出一段序列, 删除其中一段连续的子序列(或者不删), 使得剩下的序列 ...

  2. CSU 1551 Longest Increasing Subsequence Again(树状数组 或者 LIS变形)

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1551 升级版:Uva 1471 题意: 让你求删除一段连续的子序列之后的LIS. 题 ...

  3. csu 1551: Longest Increasing Subsequence Again BIT + 思维

    预处理last[i]表示以第i个开始,的合法后缀. pre[i]表示以第i个结尾,的合法前缀. 那么每一个数a[i],肯定是一个合法后缀last[i] + 一个合法前缀,那么合法前缀的数字要小于a[i ...

  4. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  5. [tem]Longest Increasing Subsequence(LIS)

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

  6. [LintCode] Longest Increasing Subsequence 最长递增子序列

    Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...

  7. Leetcode 300 Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  8. [LeetCode] Longest Increasing Subsequence

    Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...

  9. The Longest Increasing Subsequence (LIS)

    传送门 The task is to find the length of the longest subsequence in a given array of integers such that ...

随机推荐

  1. 转:百分百激活office for mac2011的激活文件

    方法:1点击finder             2点击系统盘             3点击资源库             4找到Preferences文件夹             5用压缩包里的 ...

  2. 转:iPhone libxml2 not found during build

    在新建的一个项目中,出现编译错误,发现是缺少了libxml2.dylib,后面将这个资源包添加了,编译还是出现标题上所说的问题 #import <libxml/tree.h> //#imp ...

  3. rtmutex赏析

    [摘要] rtmutex作为futex的底层实现,有两个比較重要的特性.一个是优先级继承,一个是死锁检測.本文对这两个特性的实现进行说明. 一.优先级继承 2007年火星探路者号的vxworks上发生 ...

  4. m_Orchestrate learning system---二十五、复制类的时候最容易出现的错误是什么

    m_Orchestrate learning system---二十五.复制类的时候最容易出现的错误是什么 一.总结 一句话总结:命名空间错误导致Analyze类虽然继承了Base类,但是没有执行里面 ...

  5. LSTM 时间序列数据的异常检测

    见 http://www.infoq.com/cn/articles/deep-learning-time-series-anomaly-detection 但是不够详细

  6. Xfce4里添加登录后程序自动运行

    Xfce4里添加登录后程序自动运行 (注意该方法在登录桌面环境后才会自动运行程序. 在XUbuntu下测试过, Ubuntu下应该是类似的) 方法1: 找到这个东西, 自动添加一下 方法2: 在 .c ...

  7. ASP.NET 获取IIS应用程序池的托管管道模式

    asp.net 中怎样较为简单的获取网站程序池的托管管道模式 目前已知的方式是根据这个帖子https://github.com/kakalotte/... ,利用DirectoryEntry,但是程序 ...

  8. sql点滴

    http://www.cnblogs.com/tylerdonet/p/5509398.html

  9. hiho 1068 重新整理的 Sparse-Table(RMQ)模板

    http://hihocoder.com/problemset/problem/1067代码: #include <iostream> #include <cstdio> #i ...

  10. Java排序算法(二):简单选择排序

    [基本思想] 在要排序的一组数中.选出最小的一个数与第一个位置的数交换:然后在剩下的数中再找出最小的与第二个位置的数交换,如此循环至倒数第二个数和最后一个数比較为止. 算法关键:找到最小的那个数.并用 ...