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;
using ld = long double;
const int N = 1e5 + 5;
struct line {
ll A, B;
bool operator < (const line &oth) const {
return make_pair(A, B) < make_pair(oth.A, oth.B);
}
ld ins (line oth) {
return 1.0 * (oth.B - B) / (A - oth.A);
}
ll yy (ll x) {
return A * x + B;
}
};
struct convex_hull { // finding maximum
vector<line> hull;
bool ok (line l1, line l2, line l3) {
return l1.ins(l3) < l1.ins(l2);
}
void add (line d) {
while (!hull.empty() && hull.back().A == d.A) hull.pop_back(); // hull.back().B < d.B
while (hull.size() >= 2 && ok(hull[hull.size() - 2], hull[hull.size() - 1], d)) hull.pop_back();
hull.push_back(d);
}
ll query (ll x) {
int lo = 0, hi = (int) hull.size() - 2, p = 0;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (hull[mid].ins(hull[mid + 1]) <= x) {
p = mid + 1;
lo = mid + 1;
}
else {
hi = mid - 1;
}
}
return hull[p].yy(x);
}
} H;
int n, k, a[N];
ll f[N], dp[N][225];
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
f[i] = f[i - 1] + a[i];
}
for (int i = 1; i <= n; i++) {
dp[i][1] = f[i] * (f[n] - f[i]);
}
k++;
for (int t = 2; t <= k; t++) {
convex_hull H;
for (int i = 1; i <= n; i++) {
H.add({f[i - 1], dp[i - 1][t - 1] - f[i - 1] * f[n]});
dp[i][t] = max(dp[i][t], H.query(f[i]) + f[i] * (f[n] - f[i]));
// for (int j = 1; j <= i; j++) {
// // dp[i][t] = max(dp[i][t], dp[j - 1][t - 1] + (f[i] - f[j - 1]) * (f[n] - f[i]));
// dp[i][t] = max(dp[i][t], f[j - 1] * f[i] + (dp[j - 1][t - 1] - f[j - 1] * f[n]) + f[i] * (f[n] - f[i]));
// // A x B
// }
}
}
cout << dp[n][k] << '\n';
vector<int> res;
int i = n;
for (int t = k; t >= 2; t--) {
for (int j = i; j >= 1; j--) {
if (dp[i][t] == dp[j - 1][t - 1] + (f[i] - f[j - 1]) * (f[n] - f[i])) {
i = j - 1;
res.push_back(i);
break;
}
}
}
reverse(res.begin(), res.end());
for (auto &e : res) cout << e << ' ';
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... |