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 <iostream>
#include <cstring>
using namespace std;
typedef long long LL;
typedef long double LD;
const int N = 100010, M = 210;
int n, k, p[N][M];
int q[N], hh, tt;
LL dp1[N], dp2[N]; // rolling array
LL s[N];
LD y(int i) {
return dp2[i] - s[i] * s[i];
}
LD slope(int i, int j) {
if (s[i] == s[j]) {
return -1;
}
return (1.0 * y(i) - y(j)) / ((LD)s[j] - s[i]);
}
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> s[i];
s[i] += s[i - 1];
}
for (int i = 1; i <= k; i++) {
hh = tt = 0; q[hh] = 0;
memcpy(dp2, dp1, sizeof dp2);
for (int j = 1; j <= n; j++) {
while (hh < tt && slope(q[hh], q[hh + 1]) <= s[j]) {
hh++;
}
int t = q[hh];
p[j][i] = t;
dp1[j] = dp2[t] + s[t] * (s[j] - s[t]);
while (hh < tt && slope(q[tt - 1], q[tt]) >= slope(q[tt], j)) {
tt--;
}
q[++tt] = j;
}
}
cout << dp1[n] << "\n";
while (p[n][k]) {
cout << p[n][k] << ' ';
n = p[n][k--];
}
return 0;
}
# | 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... |