# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1105089 | manhlinh1501 | K blocks (IZhO14_blocks) | C++17 | 6 ms | 41720 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>
using namespace std;
using i64 = long long;
const int MAXN = 1e5 + 5;
const int MAXK = 105;
const int LOGN = 19;
const int oo32 = 1e9 + 5;
int N, K;
int a[MAXN];
int RMQ[MAXN][LOGN];
int dp[MAXN][MAXK];
int get_max(int l, int r) {
int k = __lg(r - l + 1);
return max(RMQ[l][k], RMQ[r - (1 << k) + 1][k]);
}
namespace subtask {
void solution() {
dp[0][0] = 0;
for(int i = 1; i <= N; i++) {
for(int j = 1; j <= i; j++) {
for(int z = 1; z <= K; z++) {
dp[i][z] = min(dp[i][z], dp[j - 1][z - 1] + get_max(j, i));
}
}
}
cout << dp[N][K] << "\n";
}
}
namespace solver {
void solution() {
dp[0][0] = 0;
stack<int> S;
a[0] = oo32;
S.emplace(0);
for(int i = 1; i <= N; i++) {
while(S.size() > K and a[S.top()] <= a[i]) {
int p = S.top();
S.pop();
for(int j = 1; j <= K; j++)
dp[i][j] = min(dp[i][j], dp[p - 1][j - 1] + a[i]);
}
int p = (S.empty() ? 0 : S.top());
for(int j = 1; j <= K; j++)
dp[i][j] = min(dp[i][j], dp[p][j - 1] + a[i]);
S.emplace(i);
}
cout << dp[N][K];
}
}
signed main() {
#define task ""
if(fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N >> K;
for(int i = 1; i <= N; i++) cin >> a[i];
for(int i = 1; i <= N; i++) RMQ[i][0] = a[i];
for(int j = 1; j < LOGN; j++) {
for(int i = 1; i + (1 << j) - 1 <= N; i++)
RMQ[i][j] = max(RMQ[i][j - 1], RMQ[i + (1 << (j - 1))][j - 1]);
}
memset(dp, 0x3f, sizeof dp);
solver::solution();
return (0 ^ 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... |