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. JavaWeb利用cookie记住账号

    JavaWeb利用cookie记住账号. 首先,来看看界面什么样子. 记住账号最普遍的做法,就是在点击登录时,将账号保存到cookie中. 材料准备 <script src="${ct ...

  2. ASIHTTPRequest 框架的导入

    刚接触ios 对一切都不熟悉  记录一下ASIHTTPRequest 框架的导入 步骤 以便日后再用 1.首先下载ASIHTTPRequest:点击下载 2.在project中导入下面文件: 导入方式 ...

  3. ZOJ2326Tangled in Cables(最小生成树)

    Tangled in Cables Time Limit: 2 Seconds      Memory Limit: 65536 KB You are the owner of SmallCableC ...

  4. WAF——针对Web应用发起的攻击,包括但不限于以下攻击类型:SQL注入、XSS跨站、Webshell上传、命令注入、非法HTTP协议请求、非授权文件访问等

    核心概念 WAF Web应用防火墙(Web Application Firewall),简称WAF. Web攻击 针对Web应用发起的攻击,包括但不限于以下攻击类型:SQL注入.XSS跨站.Websh ...

  5. Django和Flask相对总结目录

    Django中文文档官网:https://yiyibooks.cn/xx/Django_1.11.6/index.html Flask中文文档官网:https://dormousehole.readt ...

  6. RelativeLayout中的baseline

    比如,加入两个相邻的TextView,给第二个TextView一个大一点的padding(比如20dp),如果加了layout_alignBaseline到第二个TextView中的话, TextVi ...

  7. 运营商 WLAN

    运营商 WLAN 运营商 WLAN 是 Android 9 中引入的一项功能,该功能可让设备自动连接到运营商实现的 WLAN 网络.在高度拥塞或信号覆盖范围较小的区域(如体育场或地铁站),运营商 WL ...

  8. nginx配置aliyun https

    server { listen 443; server_name www.goforit.com goforit.com; ssl on; ssl_certificate cert/goforit.p ...

  9. 今日SGU 5.26

    #include<bits/stdc++.h> #define de(x) cout<<#x<<"="<<x<<endl ...

  10. [HNOI2006]超级英雄(二分+网络流)

    [HNOI2006]超级英雄 题目描述 现在电视台有一种节目叫做超级英雄,大概的流程就是每位选手到台上回答主持人的几个问题,然后根据回答问题的多少获得不同数目的奖品或奖金.主持人问题准备了若干道题目, ...