Submission #1083850

#TimeUsernameProblemLanguageResultExecution timeMemory
1083850f0rizenK blocks (IZhO14_blocks)C++17
53 / 100
1038 ms4184 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; const int inf = 1e9 + 7; const ll infll = 1e18; template<typename T> istream &operator>>(istream &is, vector<T> &a) { for (auto &i : a) { is >> i; } return is; } struct Tree { using Node = int; Node pull(const Node &a, const Node &b) { return min(a, b); } int n; vector<Node> t; Tree(int _n) : n(_n) { t.resize(4 * n, inf); } Node get(int v, int l, int r, int lq, int rq) { if (rq <= l || r <= lq) { return inf; } if (lq <= l && r <= rq) { return t[v]; } int mid = (l + r) / 2; return pull(get(2 * v + 1, l, mid, lq, rq), get(2 * v + 2, mid, r, lq, rq)); } int get(int l, int r) { return get(0, 0, n, l, r); } int get(int i) { return get(i, i + 1); } void upd(int v, int l, int r, int i, int x) { if (l + 1 == r) { t[v] = x; return; } int mid = (l + r) / 2; if (i < mid) { upd(2 * v + 1, l, mid, i, x); } else { upd(2 * v + 2, mid, r, i, x); } t[v] = pull(t[2 * v + 1], t[2 * v + 2]); } void upd(int i, int x) { upd(0, 0, n, i, x); } }; int32_t main() { #ifdef LOCAL freopen("/tmp/input.txt", "r", stdin); #else ios::sync_with_stdio(false); cin.tie(nullptr); #endif int n, k; cin >> n >> k; vector<int> a(n); cin >> a; vector<int> l(n); for (int i = 0; i < n; ++i) { l[i] = i - 1; while (l[i] >= 0 && a[l[i]] <= a[i]) { l[i] = l[l[i]]; } } for (auto &i : l) { ++i; } Tree dp(n + 1); dp.upd(0, 0); for (int j = 1; j <= k; ++j) { Tree dpp(n + 1); for (int i = 1; i <= n; ++i) { dpp.upd(i, min(dpp.get(l[i - 1]), dp.get(l[i - 1], i) + a[i - 1])); } dp = dpp; } cout << dp.get(n) << "\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...