結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー auauaauaua
提出日時 2020-06-26 21:36:01
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 441 ms / 2,000 ms
コード長 1,211 bytes
コンパイル時間 1,594 ms
コンパイル使用メモリ 173,784 KB
実行使用メモリ 17,312 KB
最終ジャッジ日時 2023-09-18 03:22:59
合計ジャッジ時間 6,689 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 11 ms
4,380 KB
testcase_14 AC 11 ms
4,380 KB
testcase_15 AC 11 ms
4,384 KB
testcase_16 AC 11 ms
4,376 KB
testcase_17 AC 11 ms
4,380 KB
testcase_18 AC 11 ms
4,376 KB
testcase_19 AC 12 ms
4,380 KB
testcase_20 AC 11 ms
4,380 KB
testcase_21 AC 11 ms
4,376 KB
testcase_22 AC 11 ms
4,380 KB
testcase_23 AC 423 ms
17,172 KB
testcase_24 AC 409 ms
16,996 KB
testcase_25 AC 441 ms
16,976 KB
testcase_26 AC 440 ms
17,048 KB
testcase_27 AC 432 ms
16,968 KB
testcase_28 AC 231 ms
17,092 KB
testcase_29 AC 232 ms
17,028 KB
testcase_30 AC 314 ms
17,312 KB
testcase_31 AC 312 ms
17,168 KB
testcase_32 AC 313 ms
17,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define double long double
#define endl '\n'
typedef long long ll;
typedef pair<ll,ll> pll;
const ll inf=1e9+7;
const ll mod=1e9+7;
signed main(){
    ll n;cin>>n;
    vector<ll>a(n);
    rep(i,n)cin>>a[i];
    ll mi=inf;
    vector<ll>b(n,inf);
    vector<ll>c(n,inf);
    b[0]=a[0];
    REP(i,1,n){
        b[i]=min(b[i-1],a[i]);
    }
    c[n-1]=a[n-1];
    for(int i=n-2;i>=0;i--){
        c[i]=min(c[i+1],a[i]);
    }
    REP(i,1,n-1){
        if(a[i]>b[i-1]&&a[i]>c[i+1]){
            mi=min(mi,a[i]+b[i-1]+c[i+1]);
        }
    }
    set<ll>s,t;
    rep(i,n){
        t.insert(a[i]);
    }
    t.erase(a[0]);
    t.erase(a[1]);
    s.insert(a[0]);
    REP(i,1,n-1){
        auto itr1=s.upper_bound(a[i]);
        auto itr2=t.upper_bound(a[i]);
        if(itr1!=s.end()&&itr2!=t.end()){
            mi=min(a[i]+*itr1+*itr2,mi);
        }
        s.insert(a[i]);
        t.erase(a[i+1]);
    }
    if(mi==inf)cout<<-1<<endl;
    else cout<<mi<<endl;
}
0