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