Submission #989278

#TimeUsernameProblemLanguageResultExecution timeMemory
989278OAleksaSplit the sequence (APIO14_sequence)C++14
89 / 100
689 ms111500 KiB
#include <bits/stdc++.h> #define f first #define s second using namespace std; const int N = 1e5 + 69; const int K = 220; int n, a[N], k, par[N][K]; long long dp[N], p[N]; struct line { long long n, k; long long val(long long x) { return k * x + n; } }; deque<pair<line, int>> 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++) dp[i] = -1e18; dq[0].push_back({{0, 0}, 0}); for (int j = 1;j <= k;j++) { for (int i = 1;i < n;i++) { if (j > i) continue; int x = p[n] - p[i], m = dq[j - 1].size(); while (m > 1 && dq[j - 1][m - 1].f.val(x) <= dq[j - 1][m - 2].f.val(x)) { dq[j - 1].pop_back(); --m; } dp[i] = dq[j - 1][m - 1].f.val(x) + p[i] * (p[n] - p[i]); par[i][j] = dq[j - 1][m - 1].s; line t = {dp[i], -p[i]}; m = dq[j].size(); while (m > 1 && (dq[j][0].f.k - t.k) * (dq[j][1].f.n - dq[j][0].f.n) >= (t.n - dq[j][0].f.n) * (dq[j][0].f.k - dq[j][1].f.k)) { dq[j].pop_front(); --m; } dq[j].push_front({t, i}); } } long long ans = 0; for (int i = 1;i < n;i++) ans = max(ans, dp[i]); vector<int> res; int t = -1; for (int i = 1;i < n;i++) { if (ans == dp[i]) t = i; } assert(t != -1); while (t != 0) { res.push_back(t); t = par[t][k--]; } cout << ans << '\n'; reverse(res.begin(), res.end()); for (auto j : res) cout << j << ' '; } return 0; }

Compilation message (stderr)

sequence.cpp: In function 'int main()':
sequence.cpp:28:10: warning: variable 'Get' set but not used [-Wunused-but-set-variable]
   28 |     auto Get = [&](int l, int r) {
      |          ^~~
#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...