Submission #553535

#TimeUsernameProblemLanguageResultExecution timeMemory
553535andrei_boacaSplit the sequence (APIO14_sequence)C++17
100 / 100
872 ms106988 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll n,k,v[100005],s[100005]; int from[100005][205]; struct date { ll a,b,l,r,poz; }; deque<date> coada[205]; ll dp[205]; ll f(date F, ll x) { return F.a*x+F.b; } void out(ll i,ll val) { while(!coada[i].empty()&&coada[i].front().r<val) coada[i].pop_front(); } void add(ll i,date line) { while(!coada[i].empty()) { date prv=coada[i].back(); if(prv.a==line.a) { if(line.b>=prv.b) break; coada[i].pop_back(); continue; } ll x=(line.b-prv.b)/(prv.a-line.a); while(f(line,x)>=f(prv,x)) x++; if(x<=prv.l) { coada[i].pop_back(); continue; } coada[i].pop_back(); prv.r=x-1; line.l=x; line.r=1e18; coada[i].push_back(prv); coada[i].push_back(line); break; } if(coada[i].empty()) { line.l=0; line.r=1e18; coada[i].push_back(line); return; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin>>n>>k; k++; for(int i=1;i<=n;i++) { cin>>v[i]; s[i]=s[i-1]+v[i]; } ll ans=0; for(int i=1;i<=n;i++) { dp[1]=s[i]*s[i]; date q; q.a=-2*s[i]; q.b=s[i]*s[i]+dp[1]; q.poz=i; add(1,q); for(int j=2;j<=min(k,i*1LL);j++) { out(j-1,s[i]); date x=coada[j-1].front(); from[i][j]=x.poz; ll val=x.a*s[i]+x.b; dp[j]=s[i]*s[i]+val; q.a=-2*s[i]; q.b=s[i]*s[i]+dp[j]; q.poz=i; add(j,q); } if(i==n) ans=dp[k]; } ans=s[n]*s[n]-ans; ans/=2; cout<<ans<<'\n'; ll i=n; ll j=k; vector<ll> sol; while(i>0) { ll x=from[i][j]; if(x>0) sol.push_back(x); i=x; j--; } reverse(sol.begin(),sol.end()); for(int i:sol) cout<<i<<' '; 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...