标签:
Wavio Sequence
Wavio is a sequence of integers. It has some interesting properties.
Wavio is of odd length i.e.
L= 2n+ 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 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 le 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.
SampleInput
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
SampleOutput
9
9
1
这是一个最长上升子序列和最长下降序列的合体版, 寻找以a[i]为中心, 以 n 为半径, 左侧为升序, 右侧为降序, 求n的最大值。
普通的方法做这个会超时的, 这有最长上升子序列(LIS)长度的O(nlogn)算法
https://www.slyar.com/blog/longest-ordered-subsequence.html
#include <iostream> #include <cstdio> #include <algorithm> #include <climits> using namespace std; const int maxn = 10010; const int INF = 0x3f3f3f3f; int a[maxn], b[maxn], dp1[maxn], dp2[maxn], s[maxn]; int n; void LIS(int dp[], int tmp[]) { int top = 0; s[top] = -INF; for(int i = 1; i <= n; i++) { if(tmp[i] > s[top]) { s[++top] = tmp[i]; dp[i] = top; } else { int l = 1, r = top; while(l <= r) { int mid = (r+l)>>1; if ( tmp[i] > s[mid]) l = mid + 1; else r = mid - 1; } s[l] = tmp[i]; dp[i] = l; } } } int main() { while(~scanf("%d", &n)) { for(int i = 1; i <= n; i++) { scanf("%d", &a[i]); b[n-i+1] = a[i]; dp1[i] = dp2[i] = 0; } LIS(dp1, a); LIS(dp2, b); int ans = -1; for(int i = 1; i <= n; i++) { ans = max(min(dp1[i], dp2[n-i+1]), ans); } printf("%d\n", ans*2 - 1); } return 0; }
标签:
原文地址:http://www.cnblogs.com/cshg/p/5723418.html