Submission #586577

#TimeUsernameProblemLanguageResultExecution timeMemory
586577AlanSplit the sequence (APIO14_sequence)C++17
0 / 100
25 ms6256 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]; const ll eps = 1e-9; 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.end()[-2].cal(ps[i]) + eps > d.back().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 && d[0].sectx(d[1]) + eps > now.sectx(d[0])) d.pop_front(); d.push_front(now); } d.clear(); } auto iter = max_element(dp[k&1]+1, 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:36:24: warning: narrowing conversion of '((iter - ((ll*)(& dp[(k & 1)]))) <unknown operator> 8)' from 'long int' to 'int' [-Wnarrowing]
   36 |  vector<int> path {iter-dp[k&1]};
      |                    ~~~~^~~~~~~~
sequence.cpp:36: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...