Submission #676355

#TimeUsernameProblemLanguageResultExecution timeMemory
676355benjaminkleynSplit the sequence (APIO14_sequence)C++17
0 / 100
40 ms81816 KiB
#pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("avx,avx2,fma") #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; int N, K, a[100001]; ll pref[100001] = {0}; ll dp[100001][2] = {0}; int last_split[100001][201]; int cht[100001], fr = 0, bk = -1; #define m(j) (pref[j]) #define c(j) (dp[j][0]-pref[j]*pref[j]) inline bool bad(const int &x, const int &y, const int &z) { return (c(z) - c(y)) * (m(z) - m(x)) < (c(z) - c(x)) * (m(z) - m(y)); } inline ll eval(const int &j, const int &i) { return m(j) * pref[i] + c(j); } int main() { cin.tie(0)->sync_with_stdio(0); cin >> N >> K; for (int i = 1; i <= N; i++) { cin >> a[i]; pref[i] = pref[i-1] + a[i]; } for (int k = 1; k <= K; k++) { fr = 0; bk = -1; for (int i = k + 1; i <= N; i++) { while (fr < bk && bad(cht[bk-1], cht[bk], i-1)) bk--; cht[++bk] = i - 1; while (fr < bk && eval(cht[fr], i) <= eval(cht[fr+1], i)) fr++; dp[i][1] = eval(cht[fr], i); last_split[i][k] = cht[fr]; } for (int i = 1; i <= N; i++) dp[i][0] = dp[i][1]; } cout << dp[N][0] << '\n'; int i = N; do { i = last_split[i][K]; cout << i << ' '; } while (--K); cout << '\n'; 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...