Va 10534-wavio sequence (nlgn complexity LIS)

Source: Internet
Author: User

Click Open Link


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?


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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.