1 solutions

  • 0
    @ 2024-12-11 0:49:30

    C++ :

    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<ctime>
    #include<cmath>
    #include<string>
    #include<vector>
    #include<cstdio>
    using namespace std;
    
    const int N = 10010;
    int d[N], c1[N], c2[N];
    
    void longest_inc_seq(int* a, int n, bool f) {
        int *t = new int[n], len = 0;
        t[len] = a[0];
        f ? c1[0] = 1 : c2[0] = 1;
        for(int i = 1; i < n; ++i) {
            if(a[i] > t[len]) {
                t[++len] = a[i];
                f ? c1[i] = len + 1 : c2[i] = len + 1;
            }
            else {
                int p = lower_bound(t, t + len + 1, a[i]) - t;
                t[p] = a[i];
                f ? c1[i] = p + 1 : c2[i] = p + 1;
            }
        }
        free(t);
    }
    
    int main() {
        int n, i, j;
        while(~scanf("%d", &n)) {
            for(i = 0; i < n; ++i) scanf("%d", &d[i]), c1[i] = c2[i] = 0;
            longest_inc_seq(d, n, true);
            i = 0, j = n - 1;
            while(i < j) swap(d[i++], d[j--]);
            longest_inc_seq(d, n, false);
            int ans = 0;
            for(i = 0; i < n; ++i)
                ans = max( 2 * min(c1[i], c2[n-i-1]) - 1, ans );
            printf("%d\n", ans);
        }
        return 0;
    }
    
    
    • 1

    Information

    ID
    743
    Time
    1000ms
    Memory
    65MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By