# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
737686 | pubin06 | K blocks (IZhO14_blocks) | C++14 | 1 ms | 340 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define fi first
#define se second
#define sz(v) (int)(v).size()
using namespace std;
const int MAXn = 100005;
const int oo = 1e9;
int N, K, a[MAXn], Lf[MAXn], dp[MAXn][102];
int tmp[MAXn];
signed main(void) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
#define TASK "BLOCKS"
if (ifstream(TASK".INP")) {
freopen(TASK".INP", "r", stdin);
freopen(TASK".OUT", "w", stdout);
}
cin >> N >> K;
for (int i = 1; i <= N; i++) cin >> a[i];
stack<int> st;
a[0] = 1000001;
st.push(0);
for (int i = 1; i <= N; i++) {
while (a[st.top()] <= a[i]) st.pop();
Lf[i] = st.top();
st.push(i);
// cerr << Lf[i] << ' ';
}
// cerr << "\n\n";
tmp[0] = oo;
for (int i = 1; i <= N - K + 1; i++) dp[i][1] = max(dp[i - 1][1], a[i]);
tmp[1] = oo;
for (int i = 2; i <= N - K + 2; i++) {
if (Lf[i]) {
tmp[i] = min(tmp[Lf[i]], dp[Lf[i]][1] + a[i]);
} else {
tmp[i] = dp[1][1] + a[i];
}
}
for (int j = 2; j <= K; j++) {
for (int i = j; i <= N - K + j; i++) {
dp[i][j] = tmp[i];
}
tmp[j] = oo;
for (int i = j + 1; i <= N - K + j + 1; i++) {
if (Lf[i] >= j) {
tmp[i] = min(tmp[Lf[i]], dp[Lf[i]][j] + a[i]);
} else {
tmp[i] = dp[j][j] + a[i];
}
}
}
// for (int j = 1; j <= K; j++) {
// for (int i = 1; i <= N; i++) cerr << dp[i][j] << ' ';
// cerr << '\n';
// }
cout << dp[N][K];
return 0;
}
// Revive! >:)
// IZhO 2014 - Day 2 - Problem E
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... |