結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー hedwig100hedwig100
提出日時 2020-06-26 21:41:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 73 ms / 2,000 ms
コード長 1,244 bytes
コンパイル時間 1,899 ms
コンパイル使用メモリ 166,452 KB
実行使用メモリ 7,084 KB
最終ジャッジ日時 2023-09-18 03:40:37
合計ジャッジ時間 4,126 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 5 ms
4,380 KB
testcase_14 AC 4 ms
4,376 KB
testcase_15 AC 4 ms
4,376 KB
testcase_16 AC 4 ms
4,380 KB
testcase_17 AC 5 ms
4,380 KB
testcase_18 AC 5 ms
4,376 KB
testcase_19 AC 4 ms
4,376 KB
testcase_20 AC 5 ms
4,376 KB
testcase_21 AC 4 ms
4,376 KB
testcase_22 AC 4 ms
4,376 KB
testcase_23 AC 72 ms
6,992 KB
testcase_24 AC 72 ms
7,076 KB
testcase_25 AC 72 ms
7,056 KB
testcase_26 AC 72 ms
6,988 KB
testcase_27 AC 73 ms
6,940 KB
testcase_28 AC 73 ms
7,084 KB
testcase_29 AC 73 ms
6,988 KB
testcase_30 AC 73 ms
6,984 KB
testcase_31 AC 73 ms
7,036 KB
testcase_32 AC 73 ms
7,024 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); i ++)
using namespace std;
using ll = long long;
using PL = pair<ll,ll>;
using P = pair<int,int>;
constexpr int INF = 1000000000;
constexpr long long HINF = 1000000000000000;
constexpr long long MOD = 1000000007;
constexpr double EPS = 1e-10;
constexpr double PI = 3.14159265358979;

int main() {
    int N; cin >> N;
    vector<int> A(N);
    rep(i,N) cin >> A[i];

    vector<int> left_min(N + 1),left_max(N + 1),right_min(N + 1),right_max(N + 1);
    left_min[0] = INF; left_max[0] = 0; right_min[N] = INF; right_max[N] = 0;
    rep(i,N) {
        left_min[i + 1] = min(left_min[i],A[i]);
        left_max[i + 1] = max(left_max[i],A[i]);
    }
    for (int i = N - 1;i >= 0;--i) {
        right_min[i] = min(right_min[i + 1],A[i]);
        right_max[i] = max(right_max[i + 1],A[i]);
    }
    int ans = INF;
    rep(i,N) {
        if (0 < i < N - 1 && left_min[i] > A[i] && A[i] < right_min[i + 1]) ans = min(ans,A[i] + left_min[i] + right_min[i + 1]);
        if (0 < i < N - 1 && left_min[i] < A[i] && A[i] > right_min[i + 1]) ans = min(ans,A[i] + left_min[i] + right_min[i + 1]);
    }
    if (ans == INF) cout << -1 << endl;
    else cout << ans << endl;
    return 0;
}
0