結果

問題 No.711 競技レーティング単調増加
ユーザー finefine
提出日時 2018-06-29 23:39:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 85 ms / 2,000 ms
コード長 2,966 bytes
コンパイル時間 1,840 ms
コンパイル使用メモリ 176,980 KB
実行使用メモリ 7,396 KB
最終ジャッジ日時 2023-09-13 15:46:28
合計ジャッジ時間 5,425 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 76 ms
7,096 KB
testcase_19 AC 72 ms
7,044 KB
testcase_20 AC 77 ms
7,256 KB
testcase_21 AC 81 ms
7,368 KB
testcase_22 AC 75 ms
7,108 KB
testcase_23 AC 85 ms
7,280 KB
testcase_24 AC 84 ms
7,372 KB
testcase_25 AC 70 ms
7,100 KB
testcase_26 AC 76 ms
7,108 KB
testcase_27 AC 69 ms
7,004 KB
testcase_28 AC 63 ms
7,152 KB
testcase_29 AC 76 ms
7,368 KB
testcase_30 AC 65 ms
6,996 KB
testcase_31 AC 77 ms
7,060 KB
testcase_32 AC 81 ms
7,240 KB
testcase_33 AC 61 ms
7,236 KB
testcase_34 AC 57 ms
7,188 KB
testcase_35 AC 64 ms
7,396 KB
testcase_36 AC 73 ms
7,352 KB
testcase_37 AC 81 ms
7,272 KB
testcase_38 AC 72 ms
7,188 KB
testcase_39 AC 39 ms
5,272 KB
testcase_40 AC 36 ms
5,352 KB
testcase_41 AC 2 ms
4,380 KB
testcase_42 AC 27 ms
5,300 KB
testcase_43 AC 38 ms
6,600 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);
    
    vector<int> ans(n + 1, INF);
    queue<int> q;
    for (int i : idx) {
        if (a[i] > i) ans[i] = 0;
        while (!q.empty()) {
            int j = q.front();
            if (a[i] - a[j] >= i - j) {
                dp.update(j, ans[j]);
                q.pop();
            } else {
                break;
            }
        }
        int x = dp.query(0, i) - 1;
        ans[i] = min(ans[i], x);
        q.push(i);
    }
    cout << ans[n] + n << endl;

    /*
    vector<int> dp(n + 1, INF);
    for (int i = 0; i <= n; i++) {
        if (a[i] > i) dp[i] = i;
        for (int j = 0; j < i; j++) {
            if (a[i] - a[j] >= i - j) {
                dp[i] = min(dp[i], dp[j] + i - j - 1);
            }
        }
    }
    cout << dp[n] << endl;*/
    return 0;
}
0