Submission #108133

#TimeUsernameProblemLanguageResultExecution timeMemory
108133maruiiSplit the sequence (APIO14_sequence)C++14
100 / 100
594 ms85880 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; #define ff first #define ss second int A[100001], S[100001]; ll dp[2][100001]; pair<int, ll> line[2][100001]; int cnt[2], idx[2][100001]; int pre[201][100001]; inline ll f(int t, int i, int x){ return 1ll * line[t][i].ff * x + line[t][i].ss; } inline double crs(pair<int, ll> x, pair<int, ll> y){ return (double)(y.ss - x.ss) / (x.ff - y.ff); } int main(){ ios_base::sync_with_stdio(0), cin.tie(0); int N, K; cin>>N>>K; for(int i=0; i<N; ++i) cin>>A[i]; S[0] = A[0]; for(int i=1; i<N; ++i) S[i] = S[i-1] + A[i]; idx[1][1] = N; for(int k=0; k<=K; ++k){ int t = k&1; cnt[t] = 0; idx[!t][cnt[!t]] = N; for(int i=k, j=0; i<N; ++i){ while(idx[!t][j+1] < i && f(!t, j, S[i]) <= f(!t, j+1, S[i])) ++j; dp[t][i] = f(!t, j, S[i]); pre[k][i] = idx[!t][j]; pair<int, ll> newLine = {S[i], dp[t][i]-1ll*S[i]*S[i]}; while(cnt[t] > 1 && crs(line[t][cnt[t]-1], line[t][cnt[t]-2]) >= crs(line[t][cnt[t]-2], newLine)) --cnt[t]; idx[t][cnt[t]] = i; line[t][cnt[t]++] = newLine; } } vector<int> ans; for(int i=N-1, cnt=K; cnt; --cnt) ans.push_back(i = pre[cnt][i]); reverse(ans.begin(), ans.end()); cout<<dp[K&1][N-1]<<'\n'; for(auto i: ans) cout<<i+1<<' '; 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...