# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
319924 | tushar_2658 | Split the sequence (APIO14_sequence) | C++14 | 69 ms | 82660 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;
const int maxn = 100005;
using ll = long long;
ll dp[maxn][2];
int n, k;
ll a[maxn], pref[maxn];
int best[maxn][202];
int cur, cnt;
void compute(int l, int r, int optl, int optr){
if(l > r)return;
int mid = (l + r) >> 1;
if(optl == optr){
for(int i = l; i <= r; ++i){
dp[i][cur] = dp[optl][cur ^ 1] + pref[optl] * (pref[mid] - pref[optl]);
best[mid][cnt] = optl;
}
return;
}
int opt = optl;
for(int i = optl; i <= min(mid - 1, optr); ++i){
ll cost = dp[i][cur ^ 1] + pref[i] * (pref[mid] - pref[i]);
if(dp[mid][cur] < cost){
dp[mid][cur] = cost;
opt = i;
}
}
best[mid][cnt] = opt;
compute(l, mid - 1, optl, opt);
compute(mid + 1, r, opt, optr);
}
int main(int argc, char const *argv[])
{
scanf("%d %d", &n, &k);
for(int i = 1; i <= n; ++i){
scanf("%lld", &a[i]);
pref[i] = pref[i - 1] + a[i];
}
for(int i = 1; i <= k; ++i){
cur = i & 1;
cnt = i;
for(int j = 0; j <= n; ++j){
dp[j][cur] = -1;
}
compute(1, n, 1, n);
}
printf("%lld\n", dp[n][k & 1]);
cur = n;
int cur_k = k;
while(cur_k > 0){
printf("%d ", best[cur][cur_k]);
cur = best[cur][cur_k];
--cur_k;
}
return 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... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |