Submission #586657

#TimeUsernameProblemLanguageResultExecution timeMemory
586657AlanSplit the sequence (APIO14_sequence)C++17
100 / 100
631 ms81844 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; ll dp[2][100005], ps[100005]; int p[205][100005]; struct line { ll idx, m, c; ll cal (ll x) {return m*x + c;} ld sectx (line l) {return (ld) (l.c-c) / (m-l.m);} }; int main () { int n, k, a; cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a, ps[i] = ps[i-1] + a; deque<line> d; for (int j = 1; j <= k; j++) { d.push_back({0, 0, 0}); for (int i = 1; i <= n; i++) { while ((int) d.size() >= 2 && d.back().cal(ps[i]) <= d.end()[-2].cal(ps[i])) d.pop_back(); dp[j&1][i] = d.back().cal(ps[i]) + ps[i]*ps[n] - ps[i]*ps[i]; p[j][i] = d.back().idx; line now = {i, ps[i], dp[~j&1][i] - ps[i]*ps[n]}; while ((int) d.size() >= 2 && ((now.m == d[0].m && now.c >= d[0].c) || now.sectx(d[0]) <= d[0].sectx(d[1]))) d.pop_front(); d.push_front(now); } d.clear(); } auto iter = max_element(dp[k&1]+k, dp[k&1]+n+1); cout << *iter << "\n"; vector<int> path {iter-dp[k&1]}; for (int i = k; i > 0; i--) path.push_back(p[i][path.back()]); for (int i = k-1; i >= 0; i--) cout << path[i] << " "; cout << "\n"; return 0; }

Compilation message (stderr)

sequence.cpp: In function 'int main()':
sequence.cpp:34:24: warning: narrowing conversion of '((iter - ((ll*)(& dp[(k & 1)]))) <unknown operator> 8)' from 'long int' to 'int' [-Wnarrowing]
   34 |  vector<int> path {iter-dp[k&1]};
      |                    ~~~~^~~~~~~~
sequence.cpp:34:24: warning: narrowing conversion of '((iter - ((ll*)(& dp[(k & 1)]))) <unknown operator> 8)' from 'long int' to 'int' [-Wnarrowing]
#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...