Submission #1269435

#TimeUsernameProblemLanguageResultExecution timeMemory
1269435rayan_bdSplit the sequence (APIO14_sequence)C++20
71 / 100
255 ms196608 KiB
#include <bits/stdc++.h> using namespace std; #define int long long struct Line { mutable int k, m, p, idx; bool operator<(const Line& o) const { return k < o.k; } bool operator<(int x) const { return p < x; } }; struct LineContainer : multiset<Line, less<>> { static const int inf = LLONG_MAX; int div(int a, int b) { return a / b - ((a ^ b) < 0 && a % b); } bool isect(iterator x, iterator y) { if (y == end()) { x->p = inf; return false; } 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(int k, int 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<int,int> query(int x) { assert(!empty()); auto l = *lower_bound(x); return {l.k * x + l.m, l.idx}; } }; const long long NEG_INF = -4e18; signed main(){ ios_base::sync_with_stdio(0); cin.tie(nullptr); int n, K; cin >> n >> K; vector<int> ar(n + 1), pref(n + 1); for(int i = 1; i <= n; ++i){ cin >> ar[i]; pref[i] = pref[i - 1] + ar[i]; } vector<vector<int>> dp(n + 5, vector<int>(K + 5, NEG_INF)); vector<vector<int>> choice(n + 5, vector<int>(K + 5, -1)); for (int i = 1; i <= n+1; i++) dp[i][0] = 0; for (int k = 1; k <= K; k++) { LineContainer cht; for (int j = n; j >= 1; j--) { int slope = pref[j]; int intercept = dp[j+1][k-1] - pref[j] * pref[j]; cht.add(slope, intercept, j); int x = pref[n] + pref[j-1]; auto [val, idx] = cht.query(x); dp[j][k] = -pref[j-1] * pref[n] + val; choice[j][k] = idx; } } cout << dp[1][K] << "\n"; vector<int> cuts; int i = 1, k = K; while (k > 0 && i <= n) { int j = choice[i][k]; if (j == -1) break; cuts.push_back(j); i = j + 1; k--; } for (int x : cuts) cout << x << " "; 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...