UVa 10534 - Wavio Sequence

来源:互联网 发布:知乎 比逗 编辑:程序博客网 时间:2024/05/16 05:11
/*最长递增子序列*/#include <cstring>#include <cstdio>#include <algorithm>#include <iostream>using namespace std;const int MAXN = 10002;int* A;int B[MAXN];int C[MAXN];int n;int* d;int d1[MAXN], d2[MAXN];int M[MAXN], l;int t = 0;int bs(int i){    int x = 1, y = l+1, m;    while(x < y) {        t ++;        m = x + (y-x)/2;        if(A[M[m]] >= A[i]) {            y = m;        } else {            x = m+1;        }    }    return x;}void dp(){    int j;    l = 0;    for(int i=0; i<n; i++) {        j = bs(i) - 1;        d[i] = j+1;        M[j+1] = i;        if(j == l) ++l;    }}int main(){    #ifndef ONLINE_JUDGE    freopen("in.txt", "r", stdin);    #endif    while(scanf("%d", &n) == 1) {        for(int i=0; i<n; i++) {            scanf("%d", &B[i]);            C[n-i-1] = B[i];        }        A = B; d = d1; dp();        A = C; d = d2; dp();        int max_val = 1;        int val;        for(int i=0; i<n; i++) {            val = d1[i] < d2[n-1-i] ? d1[i] : d2[n-1-i];            if(val > max_val) max_val = val;        }        printf("%d\n", max_val*2-1);        printf("t = %d\n", t);    }    return 0;}