結果

問題 No.1188 レベルX門松列
ユーザー MZKiMZKi
提出日時 2020-08-22 16:48:13
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 319 ms / 2,000 ms
コード長 2,796 bytes
コンパイル時間 2,973 ms
コンパイル使用メモリ 174,876 KB
実行使用メモリ 16,932 KB
最終ジャッジ日時 2024-04-23 10:55:51
合計ジャッジ時間 6,004 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
7,424 KB
testcase_01 AC 226 ms
11,668 KB
testcase_02 AC 192 ms
11,016 KB
testcase_03 AC 264 ms
13,312 KB
testcase_04 AC 215 ms
16,932 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 112 ms
7,424 KB
testcase_07 AC 182 ms
12,188 KB
testcase_08 AC 182 ms
12,192 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 19 ms
5,376 KB
testcase_12 AC 19 ms
5,376 KB
testcase_13 AC 22 ms
5,376 KB
testcase_14 AC 171 ms
11,488 KB
testcase_15 AC 319 ms
16,656 KB
testcase_16 AC 4 ms
5,376 KB
testcase_17 AC 246 ms
13,952 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 224 ms
13,312 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 3 ms
5,376 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