結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー finefine
提出日時 2020-06-26 21:41:55
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 230 ms / 2,000 ms
コード長 1,823 bytes
コンパイル時間 1,750 ms
コンパイル使用メモリ 172,592 KB
実行使用メモリ 25,760 KB
最終ジャッジ日時 2023-09-18 03:41:25
合計ジャッジ時間 5,485 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 2 ms
4,380 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 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 8 ms
4,592 KB
testcase_14 AC 8 ms
4,524 KB
testcase_15 AC 7 ms
4,528 KB
testcase_16 AC 8 ms
4,540 KB
testcase_17 AC 8 ms
4,580 KB
testcase_18 AC 7 ms
4,556 KB
testcase_19 AC 7 ms
4,380 KB
testcase_20 AC 7 ms
4,528 KB
testcase_21 AC 7 ms
4,712 KB
testcase_22 AC 7 ms
4,764 KB
testcase_23 AC 226 ms
25,724 KB
testcase_24 AC 222 ms
25,760 KB
testcase_25 AC 230 ms
25,700 KB
testcase_26 AC 225 ms
25,724 KB
testcase_27 AC 228 ms
25,704 KB
testcase_28 AC 149 ms
25,728 KB
testcase_29 AC 149 ms
25,760 KB
testcase_30 AC 169 ms
25,696 KB
testcase_31 AC 167 ms
25,712 KB
testcase_32 AC 167 ms
25,760 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

constexpr char newl = '\n';
constexpr int INF = 1e9;

bool proc(int a, int b, int c, int& ans) {
    bool fmin = b < a && b < c && a != c;
    bool fmax = b > a && b > c && a != c;
    if (fmin || fmax) {
        ans = min(ans, a + b + c);
        return true;
    }
    return false;
}

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];
    }

    vector<int> Lmin(n, INF), Lmax(n, 0);
    for (int i = 1; i < n; i++) {
        Lmin[i] = min(a[i - 1], Lmin[i - 1]);
        Lmax[i] = max(a[i - 1], Lmax[i - 1]);
    }

    vector<int> Rmin(n, INF), Rmax(n, 0);
    for (int i = n - 1; i > 0; i--) {
        Rmin[i - 1] = min(Rmin[i], a[i]);
        Rmax[i - 1] = max(Rmax[i], a[i]);
    }

    int ans = INF;
    set<int> sL;
    for (int i = 0; i < n - 1; i++) {
        if (!sL.empty()) {
            if (!proc(*sL.begin(), a[i], Rmin[i], ans) && sL.size() > 1) {
                proc(*next(sL.begin()), a[i], Rmin[i], ans);
            }

            if (!proc(*sL.rbegin(), a[i], Rmax[i], ans) && sL.size() > 1) {
                proc(*prev(prev(sL.end())), a[i], Rmax[i], ans);
            }
        }
        sL.insert(a[i]);
    }

    set<int> sR;
    for (int i = n - 1; i > 0; i--) {
        if (!sR.empty()) {
            if (!proc(*sR.begin(), a[i], Lmin[i], ans) && sR.size() > 1) {
                proc(*next(sR.begin()), a[i], Lmin[i], ans);
            }

            if (!proc(*sR.rbegin(), a[i], Lmax[i], ans) && sR.size() > 1) {
                proc(*prev(prev(sR.end())), a[i], Lmax[i], ans);
            }
        }
        sR.insert(a[i]);
    }
    cout << (ans >= INF ? -1 : ans) << newl;

    return 0;
}
0