首页 > 其他 > 详细

CSUOJ 1551 Longest Increasing Subsequence Again

时间:2015-03-30 21:01:11      阅读:146      评论:0      收藏:0      [点我收藏+]

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

 

解题:线段树。。

 

技术分享
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 10005;
 4 struct node{
 5     int lt,rt,value;
 6 }tree[maxn<<2];
 7 struct Block {
 8     int lt,rt;
 9 } block[maxn];
10 void build(int lt,int rt,int v) {
11     tree[v].lt = lt;
12     tree[v].rt = rt;
13     tree[v].value = 0;
14     if(lt == rt) return;
15     int mid = (lt + rt)>>1;
16     build(lt,mid,v<<1);
17     build(mid+1,rt,v<<1|1);
18 }
19 int query(int id,int v) {
20     if(tree[v].lt == tree[v].rt) return 0;
21     int mid = (tree[v].lt + tree[v].rt)>>1;
22     if(id <= mid) return max(query(id,v<<1),tree[v<<1|1].value);
23     return query(id,v<<1|1);
24 }
25 void update(int id,int v,int value) {
26     int mid = (tree[v].lt + tree[v].rt)>>1;
27     tree[v].value = max(tree[v].value,value);
28     if(tree[v].lt == tree[v].rt) return;
29     if(id <= mid) update(id,v<<1,value);
30     else update(id,v<<1|1,value);
31 }
32 int n,m,d[maxn],discrete[maxn],width[maxn];
33 int main() {
34     while(~scanf("%d",&n)) {
35         for(int i = m = 0; i < n; ++i) {
36             scanf("%d",d+i);
37             discrete[i] = d[i];
38         }
39         sort(discrete,discrete+n);
40         int len = unique(discrete,discrete+n) - discrete;
41         build(0,len-1,1);
42         block[m].lt = block[m].rt = 0;
43         for(int i = 1; i < n; ++i)
44             if(d[i-1] < d[i]) block[m].rt++;
45             else {
46                 ++m;
47                 block[m].lt = block[m].rt=i;
48             }
49         for(int i = 0; i <= m; ++i)
50             for(int j = block[i].rt; j >= block[i].lt; --j)
51                 width[j] = block[i].rt-j+1;
52         int ans = 0;
53         for(int i = m; i >= 0; --i) {
54             for(int j = block[i].rt; j >= block[i].lt; --j) {
55                 int id = lower_bound(discrete,discrete+len,d[j])-discrete;
56                 ans = max(j - block[i].lt + 1 + query(id,1),ans);
57                 update(id,1,width[j]);
58             }
59         }
60         printf("%d\n",ans);
61     }
62     return 0;
63 }
View Code

 

CSUOJ 1551 Longest Increasing Subsequence Again

原文:http://www.cnblogs.com/crackpotisback/p/4378957.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!