# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
37531 | Just_Solve_The_Problem | Split the sequence (APIO14_sequence) | C++11 | 1179 ms | 82880 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;
#define pb push_back
#define ll long long
const int N = (int)1e5 + 7;
const ll inf = (ll)1e18 + 7;
ll dp[2][N];
ll pref[N];
int backtrack[201][N];
int n;
ll calc (int l, int r) {
return pref[l] * (pref[r] - pref[l]);
}
void div (int k, int l, int r, int optl, int optr) {
if (l > r) return;
int mid = (l + r) >> 1;
dp[k & 1][mid] = 0;
int opt = optl;
for (int i = optl; i <= min(mid - 1, optr); i++) {
ll cur = dp[k & 1 ^ 1][i] + calc(i, mid);
if (cur >= dp[k & 1][mid]) {
dp[k & 1][mid] = cur;
opt = i;
}
}
backtrack[k][mid] = opt;
div(k, l, mid - 1, optl, opt);
div(k, mid + 1, r, opt, optr);
}
main () {
int k; scanf ("%d %d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf ("%lld", pref + i);
pref[i] += pref[i - 1];
}
for (int i = 1; i <= k; i++) {
div(i, 1, n, 1, n - 1);
}
printf ("%lld\n", dp[k & 1][n]);
while (k > 0 && n > 0) {
printf ("%d ", backtrack[k][n]);
n = backtrack[k][n];
k--;
}
}
/*
7 3
4 1 3 4 0 2 3
*/
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... |