Click Open Link
Question:
Wavio sequence is a digital sequence like this:
The length is 2 * n + 1. The first n + 1 number is strictly increasing, and the last n + 1 number is strictly decreasing.
Then, let's randomly give a sequence and ask what is the maximum length of its wavio sequence subsequence?
Analysis:
For the I character, if we know 0 ~ The longest ascending sequence of I, and know that I ~ The longest descent sequence of N, then we can know the longest wavio sequence with I as the center point.
Therefore, left_up [I] indicates the longest ascending sequence length with I as the end point, and right_down [I] indicates the longest descending sequence with I as the start point.
The next step is to find the longest incrementing subsequence. Use the complexity of nlgn to find the two arrays, and then enumerate the midpoint I to calculate the answer.
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<vector>using namespace std;typedef long long int64;const int INF = 0x3f3f3f3f;const int MAXN = 10010;int n, arr[MAXN];int left_up[MAXN], right_down[MAXN];vector<int>vt;int main(){ while(~scanf("%d", &n)){ for(int i=0; i<n; ++i) scanf("%d", &arr[i]); vt.clear(); for(int i=0; i<n; ++i){ if(vt.empty() || vt.back()<arr[i]){ vt.push_back(arr[i]); }else{ int pt = lower_bound(vt.begin(), vt.end(), arr[i])-vt.begin(); vt[pt] = arr[i]; } left_up[i] = vt.size(); } vt.clear(); for(int i=n-1; i>=0; --i){ if(vt.empty() || vt.back() < arr[i]){ vt.push_back(arr[i]); }else{ int pt = lower_bound(vt.begin(), vt.end(), arr[i])-vt.begin(); vt[pt] = arr[i]; } right_down[i] = vt.size(); } int ans = 0; for(int i=0; i<n; ++i){ ans = max(ans, min(left_up[i], right_down[i])*2-1); } printf("%d\n", ans); } return 0;}