結果

問題 No.711 競技レーティング単調増加
ユーザー finefine
提出日時 2018-06-30 00:09:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 85 ms / 2,000 ms
コード長 2,363 bytes
コンパイル時間 1,766 ms
コンパイル使用メモリ 172,800 KB
実行使用メモリ 6,752 KB
最終ジャッジ日時 2023-09-13 15:53:37
合計ジャッジ時間 5,128 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 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 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 73 ms
6,380 KB
testcase_19 AC 70 ms
6,236 KB
testcase_20 AC 76 ms
6,412 KB
testcase_21 AC 79 ms
6,500 KB
testcase_22 AC 73 ms
6,300 KB
testcase_23 AC 83 ms
6,540 KB
testcase_24 AC 85 ms
6,452 KB
testcase_25 AC 67 ms
6,480 KB
testcase_26 AC 74 ms
6,300 KB
testcase_27 AC 68 ms
6,192 KB
testcase_28 AC 63 ms
6,320 KB
testcase_29 AC 74 ms
6,620 KB
testcase_30 AC 64 ms
6,284 KB
testcase_31 AC 76 ms
6,432 KB
testcase_32 AC 80 ms
6,432 KB
testcase_33 AC 61 ms
6,752 KB
testcase_34 AC 56 ms
6,596 KB
testcase_35 AC 62 ms
6,484 KB
testcase_36 AC 72 ms
6,484 KB
testcase_37 AC 80 ms
6,484 KB
testcase_38 AC 69 ms
6,392 KB
testcase_39 AC 38 ms
4,656 KB
testcase_40 AC 35 ms
4,808 KB
testcase_41 AC 2 ms
4,380 KB
testcase_42 AC 26 ms
4,656 KB
testcase_43 AC 37 ms
5,984 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const int INF = 1145141919;

template <typename T>
struct SegmentTree {
    int n;
    vector<T> data;
    T INITIAL_VALUE;

    //使うときは、この2つを適宜変更する
    static T merge(T x, T y);
    void updateNode(int k, T x);

    SegmentTree(int size, T initial_value) {
        n = 1;
        INITIAL_VALUE = initial_value;
        while (n < size) n *= 2;
        data.resize(2 * n - 1, INITIAL_VALUE);
    }

    T getLeaf(int k) {
        return data[k + n - 1];
    }

    void update(int k, T x) {
        k += n - 1; //葉の節点
        updateNode(k, x);
        while (k > 0) {
            k = (k - 1) / 2;
            data[k] = merge(data[k * 2 + 1], data[k * 2 + 2]);
        }
    }

    //区間[a, b)に対するクエリに答える
    //k:節点番号, [l, r):節点に対応する区間
    T query(int a, int b, int k, int l, int r) {
        //[a, b)と[l, r)が交差しない場合
        if (r <= a || b <= l) return INITIAL_VALUE;
        //[a, b)が[l, r)を含む場合、節点の値
        if (a <= l && r <= b) return data[k];
        else {
            //二つの子をマージ
            T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
            T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
            return merge(vl, vr);
        }
    }

    //外から呼ぶ用
    T query(int a, int b) {
        return query(a, b, 0, 0, n);
    }
};

//使うときは以下2つを変更
template <typename T>
T SegmentTree<T>::merge(T x, T y) {
    return min(x, y);
}

template <typename T>
void SegmentTree<T>::updateNode(int k, T x) {
    data[k] = x;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<int> a(n), idx(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        idx[i] = i;
    }
    a.push_back(INF);
    idx.push_back(n);

    auto comp = [&](const int i1, const int i2){
        if (a[i1] - i1 != a[i2] - i2) {
            return (a[i1] - i1) < (a[i2] - i2);
        }
        return i1 < i2;
    };

    sort(idx.begin(), idx.end(), comp);
    SegmentTree<int> dp(n + 1, INF);

    for (int i : idx) {
        int x = dp.query(0, i) - 1;
        if (a[i] > i) x = min(x, 0);
        dp.update(i, x);
    }
    cout << dp.getLeaf(n) + n << endl;
    return 0;
}
0