結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー merom686merom686
提出日時 2020-06-26 23:49:31
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 324 ms / 2,000 ms
コード長 2,163 bytes
コンパイル時間 1,196 ms
コンパイル使用メモリ 105,168 KB
実行使用メモリ 18,312 KB
最終ジャッジ日時 2023-09-18 08:53:08
合計ジャッジ時間 5,579 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 1 ms
4,380 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,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 8 ms
4,376 KB
testcase_14 AC 8 ms
4,380 KB
testcase_15 AC 8 ms
4,376 KB
testcase_16 AC 9 ms
4,376 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,384 KB
testcase_21 AC 9 ms
4,376 KB
testcase_22 AC 9 ms
4,380 KB
testcase_23 AC 315 ms
18,148 KB
testcase_24 AC 323 ms
18,148 KB
testcase_25 AC 314 ms
18,192 KB
testcase_26 AC 321 ms
18,148 KB
testcase_27 AC 324 ms
18,256 KB
testcase_28 AC 171 ms
18,184 KB
testcase_29 AC 169 ms
18,312 KB
testcase_30 AC 206 ms
18,244 KB
testcase_31 AC 214 ms
18,216 KB
testcase_32 AC 209 ms
18,236 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <array>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

template <class T>
struct SegmentTree {
    SegmentTree(int n) : n(n) {
        for (m = 2; m < n; m *= 2);
        t = vector<T>(m + n + n % 2);
    }
    T &operator[](int i) {
        return t[m + i];
    }
    const T &root() const {
        return t[1];
    }
    void build() {
        for (int i = (n - 1 + m) / 2; i > 0; i--) t[i] = t[i * 2] * t[i * 2 + 1];
    }
    void update(int i, const T &o) {
        for (t[i += m] = o; i /= 2, i > 0;) t[i] = t[i * 2] * t[i * 2 + 1];
    }
    T query(int l, int r) {
        T o0, o1;
        for (l += m, r += m; l < r; l /= 2, r /= 2) {
            if (l & 1) o0 = o0 * t[l++];
            if (r & 1) o1 = t[--r] * o1;
        }
        return o0 * o1;
    }
    vector<T> t;
    int n, m;
};
struct MaxMin {
    constexpr MaxMin() : ma(0), mi(1 << 30) {}
    MaxMin(int ma, int mi) : ma(ma), mi(mi) {}
    MaxMin operator*(const MaxMin &o) const {
        return { max(ma, o.ma), min(mi, o.mi) };
    }
    int ma, mi;
};

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

    int n;
    cin >> n;

    SegmentTree<MaxMin> st(n);

    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        st[i] = { a[i], a[i] };
    }
    st.build();

    vector<array<int, 2>> p(n, { -1, -1 });
    for (int h = 0; h < 2; h++) {
        set<int> st0;
        for (int i = 0; i < n; i++) {
            int j = h == 0 ? i : n - 1 - i;
            auto [it, b] = st0.insert(a[j]);
            it++;
            if (it != st0.end()) {
                p[j][h] = *it;
            }
        }
    }

    int r = 1 << 30;
    for (int i = 0; i < n; i++) {
        auto [ma0, mi0] = st.query(0, i);
        auto [ma1, mi1] = st.query(i + 1, n);
        if (a[i] > mi0 && a[i] > mi1) r = min(r, mi0 + a[i] + mi1);
        if (p[i][0] >= 0 && p[i][1] >= 0) {
            r = min(r, p[i][0] + a[i] + p[i][1]);
        }
    }

    cout << (r == 1 << 30 ? -1 : r) << endl;

    return 0;
}
0