Submission #208850

#TimeUsernameProblemLanguageResultExecution timeMemory
208850baohiepSplit the sequence (APIO14_sequence)C++14
100 / 100
688 ms85860 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...