CSUOJ 1551 Longest Increasing Subsequence Again

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

 

你可能感兴趣的:(sequence)