Wavio is a sequence of integers. It has some interesting properties.
Wavio is of odd length i.e. L = 2 n + 1.
The rst (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 nd 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 le contains less than 75 test cases. The description of each test case is given below. Input is terminated by end of le.
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
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
Sample Output
9
9
1
#include<iostream> #include<cstdio> #include<string> #include<cstring> #include<cmath> #include<algorithm> #include<cctype> #include <fstream> #include <limits> #include <vector> #include <list> #include <set> #include <map> #include <queue> #include <stack> #include <cassert> using namespace std; #define maxn 11000 const int finf=-1<<31; int a[maxn],b[maxn],up[maxn],down[maxn]; int main() { int n,len; while(~scanf("%d",&n)) { for(int i=1; i<=n; i++) scanf("%d",&a[i]); len=1,b[1]=a[1],up[1]=1; for(int i=2; i<=n; i++)//正向标记 { if(a[i]>b[len]) { len++; b[len]=a[i]; up[i]=len; } else { for(int j=1; j<=len; j++) if(a[i]<=b[j]) { b[j]=a[i]; up[i]=j; break; } } } len=1,b[1]=a[n],down[n]=1; for(int i=n-1; i>0; i--)//反向标记 { if(a[i]>b[len]) { len++; b[len]=a[i]; down[i]=len; } else { for(int j=1; j<=len; j++) if(a[i]<=b[j]) { b[j]=a[i]; down[i]=j; break; } } } int ans=-1; for(int i=1; i<=n; i++)//求两个标记的最大值 { ans=max(ans,min(up[i],down[i])*2-1); //cout<<up[i]<<down[i]<<endl; } printf("%d\n",ans); } return 0; }