# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1245252 | duyanhloveav | K blocks (IZhO14_blocks) | C++20 | 0 ms | 328 KiB |
#include <bits/stdc++.h>
using namespace std;
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1LL)
using ll = long long;
const int mxn = 9 + 1e6;
const int inf = 0x3f3f3f3f;
const ll lnf = 0x3f3f3f3f3f3f3f3f;
void _27augain(void) {
int n, k;
cin >> n >> k;
vector<int> a(n + 1);
vector<vector<int>> dp(k + 1, vector<int>(n + 1, inf));
for (int i = 1, m = 0; i <= n; ++i) {
cin >> a[i];
dp[1][i] = m = max(m, a[i]);
}
for (int i = 2; i <= k; ++i) {
deque<array<int, 2>> q;
for (int j = 1; j <= n; ++j) {
int v = dp[i - 1][j - 1];
while (!q.empty() && q.back()[0] < a[j]) {
v = min(v, q.back()[1]);
q.pop_back();
}
if (q.empty() || v + a[j] < q.back()[0] + q.back()[1]) {
q.push_back({a[j], v});
}
dp[i][j] = j >= i ? q.front()[0] + q.front()[1] : inf;
}
}
cout << dp[k][n] << "\n";
}
int32_t main(void) {
#define task "blocks"
cin.tie(0)->sync_with_stdio(0);
for (string iext : {"in", "inp"}) {
if (fopen((task "." + iext).c_str(), "r")) {
freopen((task "." + iext).c_str(), "r", stdin);
freopen(task ".out", "w", stdout);
}
}
int testcase = 1;
// cin >> testcase;
while (testcase--) {
_27augain();
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |