結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー MisterMister
提出日時 2020-08-01 08:03:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 253 ms / 2,000 ms
コード長 1,408 bytes
コンパイル時間 815 ms
コンパイル使用メモリ 87,092 KB
実行使用メモリ 16,304 KB
最終ジャッジ日時 2023-09-21 20:03:51
合計ジャッジ時間 4,919 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 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,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 8 ms
4,376 KB
testcase_14 AC 8 ms
4,376 KB
testcase_15 AC 8 ms
4,380 KB
testcase_16 AC 8 ms
4,380 KB
testcase_17 AC 8 ms
4,376 KB
testcase_18 AC 8 ms
4,376 KB
testcase_19 AC 8 ms
4,380 KB
testcase_20 AC 8 ms
4,376 KB
testcase_21 AC 8 ms
4,376 KB
testcase_22 AC 8 ms
4,376 KB
testcase_23 AC 246 ms
16,196 KB
testcase_24 AC 248 ms
16,180 KB
testcase_25 AC 252 ms
16,304 KB
testcase_26 AC 253 ms
16,168 KB
testcase_27 AC 252 ms
16,236 KB
testcase_28 AC 185 ms
16,260 KB
testcase_29 AC 184 ms
16,212 KB
testcase_30 AC 197 ms
16,296 KB
testcase_31 AC 199 ms
16,196 KB
testcase_32 AC 201 ms
16,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <set>

bool judge(int a, int b, int c) {
    return ((a < b && b > c) || (a > b && b < c));
}

constexpr int INF = 1 << 29;

void solve() {
    int n;
    std::cin >> n;

    std::vector<int> xs(n);
    for (auto& x : xs) std::cin >> x;

    std::vector<int>
        lsmall(n, INF), lbig(n, INF),
        rsmall(n, INF), rbig(n, INF);

    {
        int lmin = INF;
        std::set<int> s;
        for (int i = 0; i < n; ++i) {
            if (lmin < xs[i]) lsmall[i] = lmin;
            auto it = s.upper_bound(xs[i]);
            if (it != s.end()) lbig[i] = *it;

            lmin = std::min(lmin, xs[i]);
            s.insert(xs[i]);
        }
    }

    {
        int rmin = INF;
        std::set<int> s;
        for (int i = n - 1; i >= 0; --i) {
            if (rmin < xs[i]) rsmall[i] = rmin;
            auto it = s.upper_bound(xs[i]);
            if (it != s.end()) rbig[i] = *it;

            rmin = std::min(rmin, xs[i]);
            s.insert(xs[i]);
        }
    }

    int ans = INF;
    for (int i = 0; i < n; ++i) {
        ans = std::min({ans,
                        lsmall[i] + xs[i] + rsmall[i],
                        lbig[i] + xs[i] + rbig[i]});
    }

    std::cout << (ans == INF ? -1 : ans) << "\n";
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0