結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー kappybarkappybar
提出日時 2020-06-26 22:03:29
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 329 ms / 2,000 ms
コード長 1,326 bytes
コンパイル時間 1,739 ms
コンパイル使用メモリ 172,832 KB
実行使用メモリ 29,960 KB
最終ジャッジ日時 2023-09-18 04:38:56
合計ジャッジ時間 6,003 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 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 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 11 ms
4,780 KB
testcase_14 AC 11 ms
4,664 KB
testcase_15 AC 10 ms
4,676 KB
testcase_16 AC 10 ms
4,668 KB
testcase_17 AC 11 ms
4,784 KB
testcase_18 AC 11 ms
4,784 KB
testcase_19 AC 11 ms
4,744 KB
testcase_20 AC 11 ms
4,676 KB
testcase_21 AC 10 ms
4,868 KB
testcase_22 AC 11 ms
4,684 KB
testcase_23 AC 329 ms
29,960 KB
testcase_24 AC 314 ms
29,812 KB
testcase_25 AC 325 ms
29,648 KB
testcase_26 AC 321 ms
29,636 KB
testcase_27 AC 320 ms
29,692 KB
testcase_28 AC 249 ms
29,648 KB
testcase_29 AC 246 ms
29,796 KB
testcase_30 AC 272 ms
29,692 KB
testcase_31 AC 274 ms
29,704 KB
testcase_32 AC 271 ms
29,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;

int main(){
    int n;
    cin >> n;
    vector<ll> a(n);
    rep(i,n) cin >> a[i];
    vector<ll> minl(n),minr(n);
    ll mn = LINF;
    rep(i,n){
        minl[i] = mn;
        mn = min(mn,a[i]);
    }
    mn = LINF;
    for(int i=n-1;i>=0;i--){
        minr[i] = mn;
        mn = min(mn,a[i]);
    }
    ll ans = LINF;
    rep(i,n){
        if(a[i] > minl[i] && a[i] > minr[i]){
            ans = min(ans,a[i] + minr[i] + minl[i]);
        }
    }
    vector<ll> maxr(n,-1),maxl(n,-1);
    set<ll> stl;
    rep(i,n){
        auto it = stl.upper_bound(a[i]);
        if(it!=stl.end()) maxl[i] = (*it);
        stl.insert(a[i]);
    }
    set<ll> str;
    for(int i=n-1;i>=0;i--){
        auto it = str.upper_bound(a[i]);
        if(it!=str.end()) maxr[i] = (*it);
        str.insert(a[i]);
    }
    rep(i,n){
        if(a[i] < maxl[i] && a[i] < maxr[i]){
            ans = min(ans,a[i] + maxr[i] + maxl[i]);
        }
    }

    if(ans==LINF) cout << -1 << endl;
    else cout << ans << endl;
    return 0;
}
0