Submission #243999

#TimeUsernameProblemLanguageResultExecution timeMemory
243999islingrSplit the sequence (APIO14_sequence)C++14
100 / 100
497 ms81420 KiB
#include <iostream> using namespace std; const int N = 1e5 + 5, K = 205; int64_t f[2][N], a[N]; int opt[K][N], s[N]; signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; for (int i = 1; i <= n; ++i) cin >> a[i], a[i] += a[i - 1]; for (int l = 1; l <= k; ++l) { auto c = [&](int j) { return f[(l & 1) ^ 1][j] - a[j] * a[j]; }; auto val = [&](int j, int x) { return a[j] * x + c(j); }; auto cross = [&](int i, int j, int k) { return (c(k) - c(i)) * (a[j] - a[i]) - (c(j) - c(i)) * (a[k] - a[i]); }; int t = 0; for (int j = l; j <= n; ++j) { while (t > 1 && 0 <= cross(s[t - 1], s[t], j)) --t; s[++t] = j; } for (int i = l + 1, j = 1; i <= n; ++i) { while (s[j] < i && j < t && val(s[j], a[i]) < val(s[j + 1], a[i])) ++j; opt[l][i] = s[j]; f[l & 1][i] = val(s[j], a[i]); } } cout << f[k & 1][n] << '\n'; for (int v = n, l = k; v = opt[l--][v]; ) s[l] = v; for (int l = 0; l < k; ++l) cout << s[l] << ' '; }

Compilation message (stderr)

sequence.cpp: In function 'int main()':
sequence.cpp:32:27: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
  for (int v = n, l = k; v = opt[l--][v]; ) s[l] = v;
                         ~~^~~~~~~~~~~~~
#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...