Submission #989252

#TimeUsernameProblemLanguageResultExecution timeMemory
989252OAleksaSplit the sequence (APIO14_sequence)C++14
0 / 100
46 ms131072 KiB
#include <bits/stdc++.h> #define f first #define s second #define int long long using namespace std; const int N = 1e5 + 69; const int K = 220; int n, a[N], dp[N][K], p[N], k; struct line { int n, k; int val(int x) { return k * x + n; } }; deque<line> dq[K]; signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int tt = 1; //cin >> tt; while (tt--) { cin >> n >> k; for (int i = 1;i <= n;i++) cin >> a[i]; for (int i = 1;i <= n;i++) p[i] = p[i - 1] + a[i]; auto Get = [&](int l, int r) { return p[r] - p[l - 1]; }; for (int i = 1;i <= n;i++) { for (int j = 1;j <= k;j++) dp[i][j] = -1e9; } dq[0].push_back({0, 0}); for (int i = 1;i < n;i++) { for (int j = 1;j <= min(i, k);j++) { int x = p[n] - p[i], m = dq[j - 1].size(); while (m > 1 && dq[j - 1][m - 1].val(x) < dq[j - 1][m - 2].val(x)) { dq[j - 1].pop_back(); --m; } dp[i][j] = dq[j - 1][m - 1].val(x) + p[i] * (p[n] - p[i]); line t = {dp[i][j], -p[i]}; while (m > 1 && (dq[j][0].k - t.k) * (dq[j][1].n - dq[j][0].n) > (t.n - dq[j][0].n) * (dq[j][0].k - dq[j][1].k)) dq[j].pop_front(); dq[j].push_front(t); } } int ans = 0; for (int i = 1;i < n;i++) ans = max(ans, dp[i][k]); vector<int> res; int t = -1; for (int i = 1;i < n;i++) { if (ans == dp[i][k]) t = i; } assert(t != -1); res.push_back(t); for (int j = t - 1;j >= 1 && k > 0;j--) { int dd = dp[j][k - 1] + Get(j + 1, t) * Get(t + 1, n); if (dd == dp[t][k]) { res.push_back(j); --k; t = j; } } cout << ans << '\n'; reverse(res.begin(), res.end()); for (auto j : res) cout << j << ' '; } 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...