結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー uchiiiiuchiiii
提出日時 2020-07-15 12:19:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,021 bytes
コンパイル時間 3,578 ms
コンパイル使用メモリ 226,508 KB
実行使用メモリ 15,648 KB
最終ジャッジ日時 2024-05-01 11:07:29
合計ジャッジ時間 10,613 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
11,776 KB
testcase_01 AC 4 ms
6,528 KB
testcase_02 AC 4 ms
6,400 KB
testcase_03 AC 5 ms
6,400 KB
testcase_04 AC 4 ms
6,400 KB
testcase_05 AC 4 ms
6,272 KB
testcase_06 AC 5 ms
6,400 KB
testcase_07 AC 5 ms
6,400 KB
testcase_08 AC 4 ms
6,400 KB
testcase_09 AC 4 ms
6,528 KB
testcase_10 AC 4 ms
6,400 KB
testcase_11 AC 5 ms
6,400 KB
testcase_12 AC 4 ms
6,528 KB
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma region
#pragma GCC target("avx2")
#pragma GCC optimize("03")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std; typedef long double ld; typedef long long ll;
typedef unsigned long long ull;
#define endl "\n"
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define VPII vector<PII>
#define VPLL vector<PLL>
#define ALL(x) (x).begin(), (x).end()
constexpr int INF=1<<30; constexpr ll LINF=1LL<<60; constexpr ll mod=1e9+7; constexpr int NIL = -1;
template<class T>inline bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>inline bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
#pragma endregion
//-------------------

const int MX = 2e5+4;
int n; 
ll a[MX];

ll solve() {
    vector<ll> mn(MX);
    vector<ll> rmn(MX);
    ll cur = LINF;
    FOR(i,0,n-1) {
        chmin(cur, a[i]);
        mn[i] = cur;
    }
    cur = LINF;
    FOR(i,0,n-1) {
        chmin(cur, a[n-1-i]);
        rmn[n-1-i] = cur;
    }
    ll ans = LINF;
    FOR(i,1,n-2) {
        if(mn[i-1] < a[i] and a[i] > rmn[i+1]) {
            chmin(ans, a[i] + mn[i-1] + rmn[i+1]);
        }
    }
    
    set<ll> st;
    st.insert(a[0]);
    st.insert(LINF);
    FOR(i,1,n-1) {
        mn[i] = *upper_bound(ALL(st), a[i]);
        st.insert(a[i]);
    }
    st.clear();
    set<ll> rst;
    rst.insert(a[n-1]);
    rst.insert(LINF);
    for(int i=n-2; i>=0; i--) {
        rmn[i] = *upper_bound(ALL(rst), a[i]);
        rst.insert(a[i]);
    }

    FOR(i,1,n-2) {
        if(mn[i] > a[i] and a[i] < rmn[i]) {
            chmin(ans, mn[i] + a[i] + rmn[i]);
        }
    }    
    return ans;
}

int main(){
    cin.tie(0); ios::sync_with_stdio(false); //cout << fixed << setprecision(15);
    cin >> n;
    FOR(i,0,n-1) cin >> a[i];

    ll ans = solve();
    if(ans == LINF) {
        cout << -1 << endl;
        return 0;
    } else {
        cout << ans << endl;
        return 0;
    }
    return 0;
}
0