Submission #973440

#TimeUsernameProblemLanguageResultExecution timeMemory
973440TranGiaHuy1508Split the sequence (APIO14_sequence)C++17
100 / 100
726 ms90368 KiB
#include <bits/stdc++.h> using namespace std; void main_program(); signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); main_program(); } using ll = long long; const ll inf = 1e18; struct Line { mutable ll k, m, p; int idx; bool operator<(const Line& o) const { return k < o.k; } bool operator<(ll x) const { return p < x; } }; struct LineContainer : multiset<Line, less<>> { // (for doubles, use inf = 1/.0, div(a,b) = a/b) static const ll inf = LLONG_MAX; ll div(ll a, ll b) { // floored division return a / b - ((a ^ b) < 0 && a % b); } bool isect(iterator x, iterator y) { if (y == end()) return x->p = inf, 0; if (x->k == y->k) x->p = x->m > y->m ? inf : -inf; else x->p = div(y->m - x->m, x->k - y->k); return x->p >= y->p; } void add(ll k, ll m, int idx) { auto z = insert({k, m, 0, idx}), y = z++, x = y; while (isect(y, z)) z = erase(z); if (x != begin() && isect(--x, y)) isect(x, y = erase(y)); while ((y = x) != begin() && (--x)->p >= y->p) isect(x, erase(y)); } pair<ll, int> query(ll x) { assert(!empty()); auto l = *lower_bound(x); return {l.k * x + l.m, l.idx}; } }; struct StaticLineContainer : vector<Line> { int pt = 0; ll f(Line line, ll x) { return line.k * x + line.m; } bool cross(Line line1, Line line2, Line line3){ return (__int128)(line3.m - line1.m) * (line1.k - line2.k) <= (__int128)(line2.m - line1.m) * (line1.k - line3.k); } void add(ll k, ll m, int idx){ push_back({k, m, 0, idx}); int sz = size(); while (sz >= 3 && cross(at(sz - 3), at(sz - 2), at(sz - 1))){ erase(end() - 2); sz--; } } pair<ll, int> query(ll x){ if (pt >= (int)size()) pt = (int)size() - 1; while (pt < (int)size() - 1 && f(at(pt), x) < f(at(pt+1), x)) pt++; return {f(at(pt), x), at(pt).idx}; } }; int n, k; vector<ll> v, pfx; void main_program(){ cin >> n >> k; v.resize(n + 1); for (int i = 1; i <= n; i++) cin >> v[i]; pfx.resize(n + 1); for (int i = 1; i <= n; i++) pfx[i] = pfx[i-1] + v[i]; vector<ll> dp(n + 1, -inf); vector<vector<int>> trace(k + 2, vector<int>(n + 1)); dp[0] = 0; for (int j = 1; j <= k + 1; j++){ StaticLineContainer cht; vector<ll> newdp(n + 1, -inf); for (int i = 1; i <= n; i++){ // Add line (pfx[i-1] * S + dp[i-1] - pfx[i-1]^2) cht.add(pfx[i-1], dp[i-1] - pfx[i-1] * pfx[i-1], i - 1); // Find answer tie(newdp[i], trace[j][i]) = cht.query(pfx[i]); } swap(dp, newdp); } cout << dp[n] << "\n"; vector<int> splits; int crr = n; for (int j = k + 1; j > 1; j--){ crr = trace[j][crr]; splits.push_back(crr); } reverse(splits.begin(), splits.end()); for (auto elem: splits) cout << elem << " "; cout << "\n"; }
#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...