Submission #670079

#TimeUsernameProblemLanguageResultExecution timeMemory
670079JooDdaeSplit the sequence (APIO14_sequence)C++17
71 / 100
63 ms131072 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; int n, k, bt[100100][202]; ll s[100100], dp[100100][202]; array<ll, 3> t[202][100100]; int front[202], back[202]; bool check(int k, int a, int b, int c) { return (t[k][b][0]-t[k][a][0]) * (t[k][c][1]-t[k][a][1]) >= (t[k][c][0]-t[k][a][0]) * (t[k][b][1]-t[k][a][1]); } void f(int u, int k) { if(k == 0) return; f(bt[u][k], k-1); cout << u << " "; } int main(){ cin.tie(0)->sync_with_stdio(0); cin >> n >> k; for(int i=1;i<=n;i++) cin >> s[i]; for(int i=1;i<=n;i++) s[i] += s[i-1]; for(int i=0;i<=k;i++) back[i] = -1; t[0][++back[0]] = {s[n], 0, 0}; int mx = 0; for(int i=1;i<n;i++) { for(int j=min(i, k);j>=1;j--) { auto &f = front[j-1], &b = back[j-1]; while(f+1 < b && check(j-1, b-2, b-1, b)) b--, t[j-1][b] = t[j-1][b+1]; while(f < b && t[j-1][f][0]*s[i]+t[j-1][f][1] <= t[j-1][f+1][0]*s[i]+t[j-1][f+1][1]) f++; auto [X, Y, id] = t[j-1][f]; dp[i][j] = X*s[i]+Y - s[i]*s[i], bt[i][j] = id; t[j][++back[j]] = {s[n]+s[i], dp[i][j] - s[n]*s[i], i}; } if(dp[i][k] >= dp[mx][k]) mx = i; } cout << dp[mx][k] << "\n"; f(mx, k); }
#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...