Uva--10534(动规,LIS,二分)

2014-08-02 19:32:43

Problem D
Wavio Sequence 
Input: Standard Input

Output: Standard Output

Time Limit: 2 Seconds

 

Wavio is a sequence of integers. It has some interesting properties.

·  Wavio is of odd length i.e. L = 2*n + 1.

·  The first (n+1) integers of Wavio sequence makes a strictly increasing sequence.

·  The last (n+1) integers of Wavio sequence makes a strictly decreasing sequence.

·  No two adjacent integers are same in a Wavio sequence.

For example 1, 2, 3, 4, 5, 4, 3, 2, 0 is an Wavio sequence of length 9. But 1, 2, 3, 4, 5, 4, 3, 2, 2 is not a valid wavio sequence. In this problem, you will be given a sequence of integers. You have to find out the length of the longest Wavio sequence which is a subsequence of the given sequence. Consider, the given sequence as :

1 2 3 2 1 2 3 4 3 2 1 5 4 1 2 3 2 2 1.


Here the longest Wavio sequence is : 1 2 3 4 5 4 3 2 1. So, the output will be 9.

 

Input

The input file contains less than 75 test cases. The description of each test case is given below: Input is terminated by end of file.

 

Each set starts with a postive integer, N(1<=N<=10000). In next few lines there will be N integers.

 

Output

For each set of input print the length of longest wavio sequence in a line.

Sample Input                                   Output for Sample Input

10 
1 2 3 4 5 4 3 2 1 10 
19 
1 2 3 2 1 2 3 4 3 2 1 5 4 1 2 3 2 2 1 
5 
1 2 3 4 5
 
          
9 
9 
1

 

 

思路:从头开始扫一遍LIS,再从尾开始扫一遍LIS。由于这里的数据范围比较大,应该采用二分优化的LIS算法,时间复杂度为O(nlogn),具体见程序。

 1 /*************************************************************************
 2 
 3     > File Name: j.cpp
 4     > Author: Nature
 5     > Mail: 564374850@qq.com
 6     > Created Time: Thu 31 Jul 2014 12:21:05 PM CST
 7 ************************************************************************/
 8 
 9 #include <cstdio>
10 #include <cstring>
11 #include <cstdlib>
12 #include <cmath>
13 #include <iostream>
14 #include <algorithm>
15 using namespace std;
16 
17 int B_search(int v,int *s,int len){
18     int mid,l = 0,r = len;
19     while(l < r){
20         mid = l + (r - l) / 2;
21         if(s[mid] == v)
22             return mid;
23         else if(s[mid] > v)
24             r = mid;
25         else
26             l = mid + 1;
27     }
28     return l;
29 }
30 
31 int main(){
32     //freopen("in","r",stdin);
33     int n;
34     int v[10005];
35     int v2[10005];
36     int dp1[10005];
37     int dp2[10005];
38     int len1[10005];
39     int len2[10005];
40     while(scanf("%d",&n) == 1){
41         memset(dp1,0,sizeof(dp1));
42         memset(dp2,0,sizeof(dp2));
43         memset(len1,0,sizeof(len1));
44         memset(len2,0,sizeof(len2));
45         for(int i = 0; i < n; ++i){
46             scanf("%d",&v[i]);
47             v2[n - i - 1] = v[i];
48         }
49         for(int i = 0; i < n; ++i){
50             if(i == 0){// || len1[i - 1] == 1){
51                 dp1[len1[i]++] = v[i];
52             }
53             else if(dp1[len1[i - 1] - 1] < v[i]){
54                 dp1[len1[i - 1]] = v[i];
55                 len1[i] = len1[i - 1] + 1;
56             }
57             else{
58                 int pos = B_search(v[i],dp1,len1[i - 1]);
59                 //if(dp1[pos] != v[i]);
60                 dp1[pos] = v[i];
61                 len1[i] = len1[i - 1];
62             }
63         }
64         for(int i = 0; i < n; ++i){
65             if(i == 0){// || len2[i + 1] == 1){
66                 dp2[len2[i]++] = v2[i];
67             }
68             else if(dp2[len2[i - 1] - 1] < v2[i]){
69                 dp2[len2[i - 1]] = v2[i];
70                 len2[i] = len2[i - 1] + 1;
71             }
72             else{
73                 int pos = B_search(v2[i],dp2,len2[i - 1]);
74                 //if(dp2[pos] != v2[i])
75                 dp2[pos] = v2[i];
76                 len2[i] = len2[i - 1];
77             }
78         }
79         int tmax = 1;
80         for(int i = 0; i < n; ++i){
81             if(len1[i] > 1 && len2[n - i - 1] > 1 && len1[i] == len2[n - i - 1] && (len1[i] + len2[n - i - 1] - 1) % 2)
82                 tmax = max(tmax,len1[i] + len2[n - i - 1] - 1);
83         }
84         printf("%d\n",tmax);
85     }
86     return 0;
87 }

 

posted @ 2014-08-02 19:36  Naturain  阅读(132)  评论(0编辑  收藏  举报