結果

問題 No.1188 レベルX門松列
ユーザー MZKiMZKi
提出日時 2020-08-22 16:48:13
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 266 ms / 2,000 ms
コード長 2,796 bytes
コンパイル時間 2,755 ms
コンパイル使用メモリ 172,300 KB
実行使用メモリ 16,740 KB
最終ジャッジ日時 2023-08-05 13:45:26
合計ジャッジ時間 6,134 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
7,284 KB
testcase_01 AC 191 ms
11,400 KB
testcase_02 AC 186 ms
10,884 KB
testcase_03 AC 254 ms
13,096 KB
testcase_04 AC 191 ms
16,740 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 109 ms
7,244 KB
testcase_07 AC 163 ms
12,144 KB
testcase_08 AC 164 ms
12,044 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 17 ms
4,612 KB
testcase_12 AC 17 ms
4,380 KB
testcase_13 AC 19 ms
4,768 KB
testcase_14 AC 146 ms
11,248 KB
testcase_15 AC 266 ms
16,456 KB
testcase_16 AC 4 ms
4,376 KB
testcase_17 AC 202 ms
13,872 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 191 ms
12,960 KB
testcase_20 AC 2 ms
4,384 KB
testcase_21 AC 1 ms
4,384 KB
testcase_22 AC 2 ms
4,384 KB
testcase_23 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
template<class T> inline bool chmin(T&a, T b){if(a > b){a = b; return true;}else{return false;}}
template<class T> inline bool chmax(T&a, T b){if(a < b){a = b; return true;}else{return false;}}
#define ll long long
#define double long double
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=1;i<=(n);i++)
#define mod (ll)(1e9+7)
#define inf (ll)(3e18+7)
#define pi (double) acos(-1.0)
#define P pair<int,int>
#define PiP pair<ll,pair<ll,ll>>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
using namespace std;

//update(x, y) := x番目の値をyに
//query(x, y)  := [x, y)の最小値を返す

struct Segtree {
    const int INF = 0;
    int n;
    vector<int> dat;
    Segtree(int n_) : n(), dat(n_ * 4, INF) {
        int x = 1;
        while (n_ > x) {
            x *= 2;
        }
        n = x;
    }
 
    void update(int i, int x) {
        i += n - 1;
        dat[i] = x;
        while (i > 0) {
            i = (i - 1) / 2;
            dat[i] = max(dat[i * 2 + 1], dat[i * 2 + 2]);
        }
    }
 
    int query(int a, int b) { return query_sub(a, b, 0, 0, n); }
    int query_sub(int a, int b, int k, int l, int r) {
        if (r <= a || b <= l) {
            return INF;
        } else if (a <= l && r <= b) {
            return dat[k];
        } else {
            int vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
            int vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
            return max(vl, vr);
        }
    }
};

int n;
vector<int> a(100010);

int solve1(){
    Segtree seg1(n+1), seg2(n+1);
    vector<int> v1(n), v2(n);
    rep(i, n){
        v1[i] = seg1.query(0, a[i]) + 1;
        int bf = seg1.query(a[i], a[i]+1);
        seg1.update(a[i], max(v1[i], bf));
    }
    for(int i = n-1; i >= 0; i--){
        v2[i] = seg2.query(0, a[i]) + 1;
        int bf = seg2.query(a[i], a[i]+1);
        seg2.update(a[i], max(v2[i], bf));
    }
    int ans = 0;
    rep(i, n)chmax(ans, min(v1[i], v2[i])-1);
    return ans;
}

int solve2(){
    Segtree seg1(n+1), seg2(n+1);
    vector<int> v1(n), v2(n);
    rep(i, n){
        v1[i] = seg1.query(a[i]+1, n+1) + 1;
        int bf = seg1.query(a[i], a[i]+1);
        seg1.update(a[i], max(v1[i], bf));
    }
    for(int i = n-1; i >= 0; i--){
        v2[i] = seg2.query(a[i]+1, n+1) + 1;
        int bf = seg2.query(a[i], a[i]+1);
        seg2.update(a[i], max(v2[i], bf));
    }
    int ans = 0;
    rep(i, n)chmax(ans, min(v1[i], v2[i])-1);
    return ans;
}

int main(){
    cin >> n;
    map<int, int> mp1, mp2;
    rep(i, n){
        cin >> a[i];
        mp1[a[i]]++;
    }
    int now = 1;
    for(auto x : mp1){
        mp2[x.first] = now;
        now++;
    }
    rep(i, n)a[i] = mp2[a[i]];
    cout << max(solve1(), solve2()) << endl;
}   
0