結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー IKyopro
提出日時 2020-06-26 21:51:29
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 624 ms / 2,000 ms
コード長 993 bytes
コンパイル時間 2,958 ms
コンパイル使用メモリ 199,800 KB
最終ジャッジ日時 2025-01-11 11:17:37
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U> using Pa = pair<T, U>;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    vec<ll> A(N);
    ll inf = 1e18;
    ll ans = inf;
    set<ll> L,R;
    for(int i=0;i<N;i++){
        cin >> A[i];
        R.insert(A[i]);
    }
    L.insert(A[0]);
    R.erase(A[0]);
    for(int i=1;i<N-1;i++){
        R.erase(A[i]);
        ll ml = *L.begin(),mr = *R.begin();
        if(A[i]>ml && A[i]>mr) ans = min(ans,A[i]+ml+mr);
        bool ok = true;
        auto itl = L.lower_bound(A[i]);
        ok &= itl!=L.end();
        auto itr = R.lower_bound(A[i]);
        ok &= itr!=R.end();
        if(ok){
//            cerr << i << *itl << " " << *itr << "\n";
            ans = min(ans,A[i]+*itl+*itr);
        }
        L.insert(A[i]);
    }
    cout << (ans!=inf? ans:-1) << "\n";
}
0