Submission #999511

#TimeUsernameProblemLanguageResultExecution timeMemory
999511fryingducSplit the sequence (APIO14_sequence)C++17
100 / 100
1356 ms84312 KiB
#include "bits/stdc++.h" using namespace std; const int maxn = 1e5 + 5; const long long inf = 1e18; int n, a[maxn], k; int ps[maxn]; long long pref[maxn]; long long f[maxn], g[maxn]; int trace[maxn][205]; int pos, mid; long long res; int now; inline long long cost(int l, int r) { return (1ll * (1ll * ps[r] - ps[l - 1]) * (1ll * ps[r] - ps[l - 1]) - (pref[r] - pref[l - 1])) >> 1; } inline void calc(int l, int r, int optl, int optr) { if(l > r) return; res = inf; pos = -1; mid = (l + r) / 2; for(int i = min(mid, optr); i >= optl; --i) { if(g[i - 1] + cost(i, mid) < res) { res = g[i - 1] + cost(i, mid); pos = i; } } f[mid] = res; trace[mid][now] = pos - 1; if(l <= mid - 1) calc(l, mid - 1, optl, pos); if(mid + 1 <= r) calc(mid + 1, r, pos, optr); } void solve() { cin >> n >> k; for(int i = 1; i <= n; ++i) { cin >> a[i]; pref[i] = pref[i - 1] + a[i] * a[i]; ps[i] = ps[i - 1] + a[i]; } for(int i = 1; i <= n; ++i) { g[i] = cost(1, i); } long long total_sum = g[n]; for(now = 1; now <= k; ++now) { calc(1, n, 1, n); for(int j = 1; j <= n; ++j) { g[j] = f[j]; f[j] = 0; } } cout << total_sum - g[n] << '\n'; vector<int> vec; while(k) { vec.push_back(trace[n][k]); n = trace[n][k]; assert(n > 0); --k; } reverse(vec.begin(), vec.end()); for(auto i:vec) cout << i << " "; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); solve(); 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...