結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー betrue12betrue12
提出日時 2020-06-26 21:28:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 932 ms / 2,000 ms
コード長 2,809 bytes
コンパイル時間 2,718 ms
コンパイル使用メモリ 216,164 KB
実行使用メモリ 28,104 KB
最終ジャッジ日時 2023-09-18 03:02:14
合計ジャッジ時間 11,673 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 21 ms
4,376 KB
testcase_14 AC 21 ms
4,560 KB
testcase_15 AC 22 ms
4,380 KB
testcase_16 AC 22 ms
4,644 KB
testcase_17 AC 22 ms
4,672 KB
testcase_18 AC 21 ms
4,608 KB
testcase_19 AC 21 ms
4,548 KB
testcase_20 AC 21 ms
4,544 KB
testcase_21 AC 21 ms
4,684 KB
testcase_22 AC 21 ms
4,632 KB
testcase_23 AC 921 ms
27,992 KB
testcase_24 AC 932 ms
27,880 KB
testcase_25 AC 920 ms
27,956 KB
testcase_26 AC 920 ms
27,936 KB
testcase_27 AC 922 ms
27,896 KB
testcase_28 AC 422 ms
27,748 KB
testcase_29 AC 421 ms
28,104 KB
testcase_30 AC 646 ms
27,836 KB
testcase_31 AC 641 ms
27,752 KB
testcase_32 AC 636 ms
27,876 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template<typename T>
struct Segtree {
    int n, n_org;
    T e;
    vector<T> dat;
    typedef function<T(T a, T b)> Func;
    Func f;

    Segtree(){}
    Segtree(int n_input, Func f_input, T e_input){
        initialize(n_input, f_input, e_input);
    }
    void initialize(int n_input, Func f_input, T e_input){
        n_org = n_input;
        f = f_input;
        e = e_input;
        n = 1;
        while(n < n_input) n <<= 1;
        dat.assign(2*n-1, e);
    }

    void build(vector<T>& A){
        for(int k=0; k<int(A.size()); k++) dat[k+n-1] = A[k];
        for(int k=n-2; k>=0; k--) dat[k] = f(dat[2*k+1], dat[2*k+2]);
    }

    void update(int k, T a){
        k += n - 1;
        dat[k] = a;
        while(k > 0){
            k = (k - 1)/2;
            dat[k] = f(dat[2*k+1], dat[2*k+2]);
        }
    }

    T get(int k){
        return dat[k+n-1];
    }

    T between(int a, int b){
        return query(a, b+1, 0, 0, n);
    }

    T query(int a, int b, int k, int l, int r){
        if(r<=a || b<=l) return e;
        if(a<=l && r<=b) return dat[k];
        T vl = query(a, b, 2*k+1, l, (l+r)/2);
        T vr = query(a, b, 2*k+2, (l+r)/2, r);
        return f(vl, vr);
    }

    // [S, t] が条件checkを満たす最大のtを求める
    int bisect(int S, function<bool(T a)> check){
        T val = get(S);
        int k = S+n-1, l = S, r = S+1;
        while(true){
            while(k%2) k = (k-1)/2, r += r-l;
            T val2 = f(val, dat[k]);
            if(check(val2)){
                if(r == n) return n_org-1;
                val = val2;
                k++;
                int d = r-l;
                l += d, r += d;
            }else{
                break;
            }
        }
        while(k<n-1){
            T val2 = f(val, dat[2*k+1]);
            if(check(val2)){
                val = val2;
                k = 2*k+2;
                l = (l+r)/2;
            }else{
                k = 2*k+1;
                r = (l+r)/2;
            }
        }
        return min(l, n_org)-1;
    }
};

int main(){
    int N;
    cin >> N;
    vector<int> A(N);
    for(int i=0; i<N; i++) cin >> A[i];

    const int INF = 1e9;
    int ans = INF;
    for(int t=0; t<2; t++){
        map<int, vector<int>> mp;
        for(int i=0; i<N; i++) mp[(t ? A[i] : -A[i])].push_back(i);
        Segtree<int> st(N, [](int a, int b){ return min(a, b); }, INF);
        for(auto& pp : mp){
            auto& v = pp.second;
            for(int i : v){
                int a = st.between(0, i-1), b = st.between(i+1, N-1);
                if(a < INF && b < INF) ans = min(ans, a+b+A[i]);
            }
            for(int i : v) st.update(i, A[i]);
        }
    }
    if(ans == INF) ans = -1;
    cout << ans << endl;
    return 0;
}
0