結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー kyoprounokyoprouno
提出日時 2020-06-26 21:42:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,762 bytes
コンパイル時間 2,096 ms
コンパイル使用メモリ 181,444 KB
実行使用メモリ 12,796 KB
最終ジャッジ日時 2023-09-18 03:43:09
合計ジャッジ時間 4,397 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 WA -
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 WA -
testcase_09 AC 1 ms
4,376 KB
testcase_10 WA -
testcase_11 AC 1 ms
4,376 KB
testcase_12 WA -
testcase_13 AC 5 ms
4,376 KB
testcase_14 AC 5 ms
4,376 KB
testcase_15 WA -
testcase_16 AC 6 ms
4,380 KB
testcase_17 WA -
testcase_18 AC 5 ms
4,376 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 6 ms
4,376 KB
testcase_23 AC 98 ms
12,276 KB
testcase_24 AC 96 ms
12,796 KB
testcase_25 AC 96 ms
12,280 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 81 ms
12,220 KB
testcase_29 AC 80 ms
12,684 KB
testcase_30 AC 89 ms
12,680 KB
testcase_31 AC 89 ms
12,652 KB
testcase_32 AC 88 ms
12,276 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(ll i=0;i<(n);i++)
#define pll pair<ll,ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))

using namespace std;

template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}

const ll INF=1e18;

int main(){
    ll n;
    cin >> n;
    vector<ll> a(n),maxle(n),maxri(n),minle(n),minri(n),maxxri(n);
    rep(i,n){
        cin >> a[i];
    }
    ll maxr=-1,maxl=-1,minr=INF,minl=INF;
    rep(i,n){
        maxle[i]=maxl;
        maxl=max(maxl,a[i]);
        minle[i]=minl;
        minl=min(minl,a[i]);
    }
    for(ll i=n-1;i>=0;i--){
        maxri[i]=maxr;
        maxr=max(maxr,a[i]);
        minri[i]=minr;
        minr=min(minr,a[i]);
    }
    maxxri=maxri;
    sort(maxxri.begin(),maxxri.end());
    ll ans=INF;
    for(ll i=1;i<n-1;i++){
        if(maxle[i]>a[i] && maxri[i]>a[i]){
            ll le=i,ri=n;
            while(ri-le>1){
                ll mid=(ri+le)/2;
                if(maxri[mid]>a[i]) ri=mid;
                else le=mid;
            }
            auto it=upper_bound(maxle.begin(),maxle.end(),a[i])-maxle.begin();
            ll val1=maxle[it];
            ll val2=maxri[ri];
            ans=min(ans,val1+val2+a[i]);
        }
        if(minle[i]<a[i] && minri[i]<a[i]){
            ans=min(ans,minle[i]+minri[i]+a[i]);
        }
    }
    if(ans==INF) cout << -1 << endl;
    else cout << ans << endl;
}
0