結果

問題 No.1077 Noelちゃんと星々4
ユーザー tsugutsugutsugutsugu
提出日時 2023-06-19 00:20:40
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,284 ms / 2,000 ms
コード長 2,433 bytes
コンパイル時間 3,035 ms
コンパイル使用メモリ 253,304 KB
実行使用メモリ 135,496 KB
最終ジャッジ日時 2023-09-08 17:56:53
合計ジャッジ時間 16,576 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
4,380 KB
testcase_01 AC 13 ms
4,624 KB
testcase_02 AC 885 ms
93,816 KB
testcase_03 AC 1,136 ms
120,556 KB
testcase_04 AC 344 ms
38,392 KB
testcase_05 AC 256 ms
29,664 KB
testcase_06 AC 445 ms
49,032 KB
testcase_07 AC 508 ms
56,216 KB
testcase_08 AC 372 ms
41,652 KB
testcase_09 AC 322 ms
36,312 KB
testcase_10 AC 1,144 ms
120,848 KB
testcase_11 AC 740 ms
79,144 KB
testcase_12 AC 612 ms
65,736 KB
testcase_13 AC 242 ms
28,404 KB
testcase_14 AC 1,114 ms
117,928 KB
testcase_15 AC 583 ms
63,564 KB
testcase_16 AC 382 ms
42,692 KB
testcase_17 AC 668 ms
71,752 KB
testcase_18 AC 1,162 ms
122,896 KB
testcase_19 AC 232 ms
27,116 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 AC 1,284 ms
135,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "debug.hpp"
#else
#define debug(...) 1
#endif

template <typename T>
struct Segtree {
    int N = 1;
    T e;
    vector<T> dat;
    function<T(T, T)> op;
    function<T(T, T)> update;
    Segtree(int N_, T e_, function<T(T, T)> op_, function<T(T, T)> update_) : e(e_), op(op_), update(update_) {
        int sz = 1;
        while (sz < N_) {
            sz <<= 1;
        }
        dat.resize(2 * sz + 1, e);
        N = sz;
    } 
    Segtree(vector<T> &dat_, T e_, function<T(T, T)> op_, function<T(T, T)> update_) : e(e_), op(op_), update(update_) {
        int N_ = dat_.size();
        int sz = 1;
        while (sz < N_) {
            sz <<= 1;
        }
        dat.resize(2 * sz + 1, e);
        N = sz;
        for (int i = 0; i < (int) dat_.size(); i++) {
            dat[i + N] = dat_[i];
        }
        build();
    }

    void build() {
        for (int i = N - 1; i >= 1; i--) {
            dat[i] = op(dat[(i << 1) | 0], dat[(i << 1) | 1]);
        }
    }
    T query_all() {
        return dat[1];
    }
    // [l, r)
    T query(int l, int r) {
        assert(0 <= l && r <= N);
        T ret = e;
        l += N, r += N;
        while (l < r) {
            if (l & 1) {
                ret = op(ret, dat[l++]);
            }
            if (r & 1) {
                ret = op(ret, dat[--r]);
            }
            l >>= 1, r >>= 1;
        }
        return ret;
    }

    void modify(int i, T x) {
        assert(i >= 0 && i < N);
        i += N;
        dat[i] = update(dat[i], x);
        while (i > 1) {
            i >>= 1;
            dat[i] = op(dat[(i << 1) | 0], dat[(i << 1) | 1]);
        }
    }

    T operator[] (int i) {
        return dat[i + N - 1];
    }
};

const int mxY = 10005;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<Segtree<int>> dp(n + 1, Segtree<int>(mxY, 1 << 30, [](int a, int b) {return min(a, b);}, [](int a, int b) {return b;}));
    dp[0].modify(0, 0);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < mxY; j++) {
            int val = dp[i].query(0, j + 1) + abs(a[i] - j);
            if (dp[i + 1][j] > val) {
                dp[i + 1].modify(j, val);
            }
        }
    }
    int ans = dp[n].query_all();
    cout << ans << '\n';
}
0