結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー hanbei_dayo
提出日時 2020-07-16 10:58:46
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,470 bytes
コンパイル時間 991 ms
コンパイル使用メモリ 108,908 KB
実行使用メモリ 17,376 KB
最終ジャッジ日時 2024-11-24 07:53:44
合計ジャッジ時間 6,052 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19 WA * 11
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<functional>
#include<math.h>
#include<random>
#include <bitset>
using namespace std;
#define N (1000000000+7)
//#define N 998244353
#define INF 1e16
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> Q;

const int inf = (int)1e9; 

ll maxl[200010];
ll maxr[200010];

int main(void){
    int n;
    cin>>n;
    vector<ll>a(n);
    for(int i=0;i<n;i++)cin>>a[i];
    maxl[0]=0;
    for(int i=0;i<n;i++){
        maxl[i+1]=max(maxl[i],a[i]);
    }
    reverse(a.begin(),a.end());
    for(int i=0;i<n;i++){
        maxr[i+1]=max(maxr[i],a[i]);
    }
    reverse(a.begin(),a.end());
    set<ll>s1,s2;
    s1.insert(a[0]);
    for(int i=1;i<n;i++)s2.insert(a[i]);
    ll ans = (ll)INF;
    for(int i=1;i<n;i++){
        ll l = maxl[i];
        ll r = maxr[n-1-i];
        if((l>a[i])&&(a[i]<r)){
            ans = min(ans,l+r+a[i]);
        }
        s2.erase(a[i]);
        auto itr1 = s1.lower_bound(a[i]);
        auto itr2 = s2.lower_bound(a[i]);
        if(itr1==s1.begin()){
            s1.insert(a[i]);
            continue;
        }
        if(itr2==s2.begin()){
            s1.insert(a[i]);
            continue;
        }
        ans = min(ans,a[i]+*(s1.begin())+*(s2.begin()));
        s1.insert(a[i]);
    }
    if(ans == (ll)INF)cout<<-1<<endl;
    else cout<<ans<<endl;
    return 0;
}
0