Submission #208839

#TimeUsernameProblemLanguageResultExecution timeMemory
208839baohiepSplit the sequence (APIO14_sequence)C++14
0 / 100
60 ms84336 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), id(_id) {} ll calc(ll x) { return a * (x - a) + 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(); dq.emplace_back(sum[1], 0, 1); for (int j = 2; j <= n; ++j) { while (seen + 1 < (int)dq.size() && dq[seen].calc(sum[j]) <= dq[seen + 1].calc(sum[j])) ++seen; 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() > seen + 1 && intersect(dq[(int)dq.size() - 2], dq.back()) >= intersect(dq.back(), newLine)) dq.pop_back(); dq.push_back(newLine); } for (int j = 2; j <= n; ++j) pre[j] = dp[j]; } cout << dp[n] << "\n"; int cur = n; while (k) { cur = trace[cur][k--]; cout << cur << " "; } 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...