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 int N = 1e5 + 5, K = 205;
struct Line {
ll a, b;
int id;
Line(ll _a, ll _b, int _id): a(_a), b(_b - _a * _a), id(_id) {}
ll calc(ll x) {
return a * x + b;
}
};
int n, k, a[N], trace[N][K];
ll sum[N], pre[N], dp[N];
vector<Line> dq;
double intersect(const Line &p, const Line &q) {
return (double)(p.b - q.b) / (double)(q.a - p.a);
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
sum[i] = sum[i - 1] + a[i];
}
for (int i = 1; i <= k; ++i) {
int seen = 0;
dq.clear();
fill(dp + 1, dp + 1 + n, 0);
for (int j = 1; j <= n; ++j) {
while (seen + 1 < (int)dq.size() && dq[seen].calc(sum[j]) <= dq[seen + 1].calc(sum[j]))
++seen;
if (seen < (int)dq.size()) {
dp[j] = dq[seen].calc(sum[j]);
trace[j][i] = dq[seen].id;
}
Line newLine = Line(sum[j], pre[j], j);
while ((int)dq.size() > 1 && intersect(dq[(int)dq.size() - 2], newLine) <= intersect(dq[(int)dq.size() - 2], dq.back()))
dq.pop_back();
dq.push_back(newLine);
}
for (int j = 1; j <= n; ++j) pre[j] = dp[j];
}
int cur = n;
vector<int> pos;
while (k) {
cur = trace[cur][k--];
pos.emplace_back(cur);
}
reverse(pos.begin(), pos.end());
cout << dp[n] << "\n";
for (int x: pos) cout << x << " ";
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... |