# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
121364 | IOrtroiii | Split the sequence (APIO14_sequence) | C++14 | 433 ms | 85136 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 ll = long long;
const ll inf = 1e18;
struct Line {
int a; ll b;
Line(int a = 0, ll b = 0) : a(a), b(b) {}
ll f(int x) { return (ll) a * x + b; }
};
bool bad(Line x, Line y, Line z) {
return (ll) (x.b - z.b) * (y.a - x.a) <= (ll) (z.a - x.a) * (x.b - y.b);
}
const int N = 100100;
struct Hull {
Line ls[N];
int go[N];
int h, t;
void reset() { h = t = 0; }
void add(Line z, int id) {
while (h + 1 < t && bad(ls[t - 2], ls[t - 1], z)) {
t--;
}
ls[t] = z;
go[t++] = id;
}
int trace() {
return go[h];
}
ll get(int x) {
while (h + 1 < t && ls[h].f(x) <= ls[h + 1].f(x)) {
++h;
}
return ls[h].f(x);
}
} hull[2];
ll f[2][N];
int a[N];
int go[205][N];
int ans[205];
int main() {
int n, k;
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; ++i) {
scanf("%d", a + i);
a[i] += a[i - 1];
}
int now = 0, nxt = 1;
for (int j = 1; j <= k; ++j) {
hull[nxt].reset();
for (int i = 1; i <= n; ++i) {
f[nxt][i] = hull[now].get(a[i]);
go[j][i] = hull[now].trace();
hull[now].add(Line(a[i], f[now][i] - (ll) a[i] * a[i]), i);
}
swap(now, nxt);
}
printf("%lld\n", f[now][n]);
now = n;
for (int i = k; i > 0; --i) {
now = go[i][now];
ans[i] = now;
}
for (int i = 1; i <= k; ++i) {
printf("%d ", ans[i]);
}
}
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... |