結果
問題 |
No.1077 Noelちゃんと星々4
|
ユーザー |
|
提出日時 | 2023-06-19 00:20:40 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,287 ms / 2,000 ms |
コード長 | 2,433 bytes |
コンパイル時間 | 2,899 ms |
コンパイル使用メモリ | 257,260 KB |
実行使用メモリ | 135,552 KB |
最終ジャッジ日時 | 2024-06-26 10:43:49 |
合計ジャッジ時間 | 16,346 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 20 |
ソースコード
#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'; }