//Bismillahir Rahmanir Rahim
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
const int maxn = 1e5 + 10;
int n, kk, arr[maxn], rmq[maxn][20], lg[maxn];
int dp[101][maxn];
int Max(int i, int j) {
int l = lg[j-i+1];
return max(rmq[i][l], rmq[j - (1 << l) + 1][l]);
}
void solve(int k, int l, int r, int opl, int opr) {
if(l > r) return;
int mid = l + r >> 1, idx = -1;
for(int j = opl; j <= opr && j < mid; j++) {
int c = dp[k-1][j] + Max(j + 1, mid);
if(dp[k][mid] > c)
dp[k][mid] = c, idx = j;
}
solve(k, l, mid-1, opl, idx);
solve(k, mid+1, r, idx, opr);
}
int main(int argc, char const *argv[]) {
#ifdef LOCAL_TESTING
freopen("in", "r", stdin);
#endif
cin>>n>>kk;
for(int i=1; i<=n; i++) {
cin>>arr[i];
rmq[i][0] = arr[i];
} lg[1] = 0;
for(int i = 2; i <= n; i++)
lg[i] = lg[i>>1] + 1;
for(int i = 1; i<=20; i++)
for(int j=1; j <= n; j++)
if(j + (1 << (i-1)) <= n)
rmq[j][i] = max(rmq[j][i-1], rmq[j + (1 << (i-1))][i-1]);
memset(dp, 0x3f, sizeof dp);
for(int i=1; i<=n; i++)
dp[1][i] = Max(1, i);
for(int k = 2; k <= kk; k++)
solve(k, 1, n, 1, n);
cout<<dp[kk][n]<<endl;
}
Compilation message
blocks.cpp: In function 'void solve(int, int, int, int, int)':
blocks.cpp:16:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l + r >> 1, idx = -1;
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
50072 KB |
Output is correct |
2 |
Correct |
6 ms |
50072 KB |
Output is correct |
3 |
Correct |
6 ms |
50072 KB |
Output is correct |
4 |
Correct |
0 ms |
50072 KB |
Output is correct |
5 |
Correct |
3 ms |
50072 KB |
Output is correct |
6 |
Correct |
6 ms |
50072 KB |
Output is correct |
7 |
Correct |
0 ms |
50072 KB |
Output is correct |
8 |
Correct |
3 ms |
50072 KB |
Output is correct |
9 |
Correct |
0 ms |
50072 KB |
Output is correct |
10 |
Correct |
6 ms |
50072 KB |
Output is correct |
11 |
Incorrect |
3 ms |
50072 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
50072 KB |
Output is correct |
2 |
Correct |
3 ms |
50072 KB |
Output is correct |
3 |
Correct |
3 ms |
50072 KB |
Output is correct |
4 |
Correct |
6 ms |
50072 KB |
Output is correct |
5 |
Correct |
3 ms |
50072 KB |
Output is correct |
6 |
Correct |
0 ms |
50072 KB |
Output is correct |
7 |
Correct |
9 ms |
50072 KB |
Output is correct |
8 |
Correct |
3 ms |
50072 KB |
Output is correct |
9 |
Correct |
0 ms |
50072 KB |
Output is correct |
10 |
Correct |
6 ms |
50072 KB |
Output is correct |
11 |
Incorrect |
0 ms |
50072 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
50072 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
50072 KB |
Output is correct |
2 |
Correct |
23 ms |
50072 KB |
Output is correct |
3 |
Correct |
59 ms |
50072 KB |
Output is correct |
4 |
Incorrect |
309 ms |
50072 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |