# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1105096 | manhlinh1501 | K blocks (IZhO14_blocks) | C++17 | 366 ms | 50464 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;
#define left ___left
int N, K;
int a[MAXN];
int RMQ[MAXN][LOGN];
int dp[MAXN][MAXK];
int left[MAXN];
int get_min(int l, int r) {
int k = __lg(r - l + 1);
return min(RMQ[l][k], RMQ[r - (1 << k) + 1][k]);
}
namespace solver {
void solution() {
dp[0][0] = 0;
stack<int> S;
int maxx = 0;
for(int i = 1; i <= N; i++)
dp[i][1] = (maxx = max(maxx, a[i]));
for(int j = 2; j <= K; j++) {
for(int i = 1; i <= N; i++)
RMQ[i][0] = dp[i][j - 1];
for(int j = 1; j < LOGN; j++) {
for(int i = 1; i + (1 << j) - 1 <= N; i++)
RMQ[i][j] = min(RMQ[i][j - 1], RMQ[i + (1 << (j - 1))][j - 1]);
}
for(int i = 1; i <= N; i++) {
if(left[i] < i)
dp[i][j] = min(dp[i][j], get_min(left[i], i - 1) + a[i]);
dp[i][j] = min(dp[i][j], min(dp[left[i] - 1][j], dp[left[i] - 1][j - 1] + a[i]));
}
}
// for(int i = 1; i <= N; i++) {
// for(int j = 1; j <= K; j++)
// cerr << i << " " << j << " " << dp[i][j] << "\n";
// }
cout << dp[N][K];
}
}
signed main() {
#define task "code"
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++) {
left[i] = i - 1;
while(left[i] > 0 and a[left[i]] <= a[i])
left[i] = left[left[i]];
}
for(int i = 1; i <= N; i++) left[i]++;
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... |