Submission #958131

#TimeUsernameProblemLanguageResultExecution timeMemory
958131MinaRagy06Split the sequence (APIO14_sequence)C++17
100 / 100
486 ms85292 KiB
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; #define ll long long const int N = 100'005; const ll inf = 1e18; int a[N]; ll prf[N]; int prv[205][N]; array<ll, 2> dp[2][N]; int main() { ios_base::sync_with_stdio(0), cin.tie(0); int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> a[i]; prf[i] = prf[i - 1] + a[i]; } m++; for (int i = 1; i <= n; i++) dp[0][i] = {-inf, -1}; for (int j = 1; j <= m; j++) { dp[j & 1][0] = {-inf, -1}; auto cmp = [&] (int x, int y, int z) { return (__int128_t) (dp[(j - 1) & 1][x][0] - dp[(j - 1) & 1][y][0] - prf[x] * prf[x] + prf[y] * prf[y]) * (prf[z] - prf[y]) >= (__int128_t) (dp[(j - 1) & 1][y][0] - dp[(j - 1) & 1][z][0] - prf[y] * prf[y] + prf[z] * prf[z]) * (prf[y] - prf[x]); }; auto calc = [&] (int i, int k) { return dp[(j - 1) & 1][k][0] + prf[k] * (prf[i] - prf[k]); }; // {x, y, z} deque<int> dq; int sz = 0; for (int i = 1; i <= n; i++) { // cout << i << ' ' << sz << endl; while (sz > 1 && cmp(dq[sz - 2], dq[sz - 1], i - 1)) { dq.pop_back(); sz--; } dq.push_back(i - 1); sz++; while (sz > 1 && calc(i, dq[0]) <= calc(i, dq[1])) { dq.pop_front(); sz--; } dp[j & 1][i] = {calc(i, dq[0]), dq[0]}; } for (int i = 1; i <= n; i++) { prv[j][i] = dp[j & 1][i][1]; } } cout << dp[m & 1][n][0] << '\n'; int j = m, i = n; vector<int> ans; while (j > 1) { ans.push_back(prv[j][i]); i = prv[j][i]; j--; } reverse(ans.begin(), ans.end()); for (auto x : ans) { cout << x << ' '; } cout << '\n'; 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...