結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー minatominato
提出日時 2020-06-26 21:26:30
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 2,600 bytes
コンパイル時間 3,316 ms
コンパイル使用メモリ 234,876 KB
実行使用メモリ 6,176 KB
最終ジャッジ日時 2023-09-18 02:58:21
合計ジャッジ時間 5,394 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 4 ms
4,376 KB
testcase_14 AC 4 ms
4,380 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 4 ms
4,504 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 4 ms
4,380 KB
testcase_19 AC 4 ms
4,380 KB
testcase_20 AC 4 ms
4,376 KB
testcase_21 AC 4 ms
4,376 KB
testcase_22 AC 4 ms
4,376 KB
testcase_23 AC 48 ms
6,024 KB
testcase_24 AC 49 ms
5,952 KB
testcase_25 AC 50 ms
5,900 KB
testcase_26 AC 49 ms
6,036 KB
testcase_27 AC 49 ms
5,920 KB
testcase_28 AC 49 ms
5,956 KB
testcase_29 AC 50 ms
6,176 KB
testcase_30 AC 50 ms
5,952 KB
testcase_31 AC 49 ms
6,024 KB
testcase_32 AC 49 ms
6,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using u64 = uint_fast64_t;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
constexpr char ln =  '\n';
constexpr long long MOD = 1000000007;
//constexpr long long MOD = 998244353;
template<class T1, class T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return true;} return false; }
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return true;} return false; }
inline int popcount(int x) {return __builtin_popcount(x);}
inline int popcount(long long x) {return __builtin_popcountll(x);}
void print() { cout << "\n"; }
template<class T, class... Args>
void print(const T &x, const Args &... args) {
    cout << x << " ";
    print(args...);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////


template<typename T>
struct SegmentTree {
    const T inf = numeric_limits<T>::max();
    int N;
    vector<T> node;

    SegmentTree() = default;
    SegmentTree(vector<T> &v) {init(v);}

    void init(vector<T> &v) {
        int sz = v.size();
        N = 1; while (N < sz) N *= 2;
        node.assign(2*N,inf);

        for (int i = 0; i < sz; i++) node[i+N] = v[i];
        for (int i = N-1; i > 0; i--) node[i] = min(node[i<<1|0],node[i<<1|1]);
    }

    void update(int x, T val) { // (0-indexed)
        x += N;

        node[x] = val;
        while (x > 1) {
            x >>= 1;
            node[x] = min(node[x<<1|0],node[x<<1|1]);
        }
    }

    T get(int l, int r) { // [l,r) (0-indexed)
        T res = inf;
        l += N; r += N;
        while (l < r) {
            if (l & 1) res = min(res,node[l++]);
            l >>= 1;
            if (r & 1) res = min(res,node[--r]);
            r >>= 1; 
        }

        return res;
    }

    T operator[](int x) {return node[x+N];}  
};

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int N; cin >> N;
    vector<int> A(N);
    rep(i,N) cin >> A[i];

    SegmentTree<int> seg(A);
    int ans = 1e9;
    for (int i = 1; i < N-1; i++) {
        int l = seg.get(0,i);
        int r = seg.get(i+1,N);
        if (l < A[i] and r < A[i]) {
            chmin(ans,l+A[i]+r);
        }
        if (l > A[i] and r > A[i]) {
            chmin(ans,l+A[i]+r);
        }
    }

    cout << (ans==1e9 ? -1 : ans) << ln;
}
0