結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー karinohito
提出日時 2024-09-11 16:48:25
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 727 ms / 2,000 ms
コード長 1,061 bytes
コンパイル時間 2,057 ms
コンパイル使用メモリ 204,072 KB
最終ジャッジ日時 2025-02-24 06:40:43
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll=long long;

int cnt(string S){
    int k=0,res=0;
    for(auto s:S){
        if(s=='o')k++;
        else{
            res=max(res,k);
            k=0;
        }
    }
    return max(res,k);
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    
    int N;
    cin>>N;
    vector<int> A(N);
    for(int i=0;i<N;i++)cin>>A[i];
    set<int> L,R;
    for(int i=1;i<N;i++)R.insert(A[i]);
    L.insert(A[0]);
    int an=1e9+10;
    for(int i=1;i<N-1;i++){
        R.erase(A[i]);
        L.insert(A[i]);
        auto r=R.upper_bound(A[i]);
        auto l=L.upper_bound(A[i]);
        if(r==R.end()||l==L.end())continue;
        an=min(an,A[i]+(*r)+(*l));
    }
    L.clear();
    R.clear();
    L.insert(A[0]);
    for(int i=1;i<N;i++)R.insert(A[i]);
    for(int i=1;i<N-1;i++){
        R.erase(A[i]);
        L.insert(A[i]);
        auto r=*(R.begin());
        auto l=*(L.begin());
        if(r>=A[i]||l>=A[i])continue;
        an=min(an,A[i]+r+l);
    }
    cout<<(an<1e9?an:-1)<<endl;

}
0