結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー chocoruskchocorusk
提出日時 2020-06-27 12:37:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 334 ms / 2,000 ms
コード長 1,778 bytes
コンパイル時間 1,493 ms
コンパイル使用メモリ 137,200 KB
実行使用メモリ 15,608 KB
最終ジャッジ日時 2023-09-18 19:17:52
合計ジャッジ時間 5,588 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,384 KB
testcase_13 AC 10 ms
4,380 KB
testcase_14 AC 10 ms
4,376 KB
testcase_15 AC 10 ms
4,380 KB
testcase_16 AC 10 ms
4,376 KB
testcase_17 AC 9 ms
4,376 KB
testcase_18 AC 10 ms
4,380 KB
testcase_19 AC 10 ms
4,376 KB
testcase_20 AC 10 ms
4,376 KB
testcase_21 AC 10 ms
4,380 KB
testcase_22 AC 10 ms
4,376 KB
testcase_23 AC 324 ms
15,460 KB
testcase_24 AC 332 ms
15,608 KB
testcase_25 AC 324 ms
15,460 KB
testcase_26 AC 332 ms
15,464 KB
testcase_27 AC 334 ms
15,496 KB
testcase_28 AC 230 ms
15,500 KB
testcase_29 AC 232 ms
15,464 KB
testcase_30 AC 254 ms
15,592 KB
testcase_31 AC 255 ms
15,536 KB
testcase_32 AC 253 ms
15,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <list>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const int INF=1e9+7;
int solve1(int n, vector<int> a){
    int mnl[200020], mnr[200020];
    mnl[0]=INF, mnr[n]=INF;
    for(int i=0; i<n; i++){
        mnl[i+1]=min(mnl[i], a[i]);
    }
    for(int i=n-1; i>=0; i--){
        mnr[i]=min(mnr[i+1], a[i]);
    }
    int ans=INF;
    for(int i=0; i<n; i++){
        if(mnl[i]<a[i] && mnr[i+1]<a[i]) ans=min(ans, a[i]+mnl[i]+mnr[i+1]);
    }
    return ans;
}
int solve2(int n, vector<int> a){
    int mnl[200020], mnr[200020];
    set<int> st; st.insert(a[0]);
    for(int i=1; i<n-1; i++){
        auto itr=st.upper_bound(a[i]);
        if(itr==st.end()) mnl[i]=INF;
        else mnl[i]=(*itr);
        st.insert(a[i]);
    }
    st.clear();
    st.insert(a[n-1]);
    for(int i=n-2; i>=1; i--){
        auto itr=st.upper_bound(a[i]);
        if(itr==st.end()) mnr[i]=INF;
        else mnr[i]=(*itr);
        st.insert(a[i]);
    }
    int ans=INF;
    for(int i=1; i<n-1; i++){
        ans=min(ans, a[i]+mnl[i]+mnr[i]);
    }
    return ans;
}
int main()
{
    int n;
    cin>>n;
    vector<int> a(n);
    for(int i=0; i<n; i++) cin>>a[i];
    int ans=solve1(n, a);
    ans=min(ans, solve2(n, a));
    if(ans>=INF) cout<<-1<<endl;
    else cout<<ans<<endl;
    return 0;
}
0