This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//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 (stderr)
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 |
---|
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... |