Submission #337426

#TimeUsernameProblemLanguageResultExecution timeMemory
337426ogibogi2004Split the sequence (APIO14_sequence)C++14
71 / 100
143 ms131076 KiB
#include<bits/stdc++.h> using namespace std; #define ld long double #define ll long long const ld INF=2e9; const ll MAXN=1e5+6; const ll MAXK=210; ll dp[MAXN][MAXK],a[MAXN]; struct line { ld a,b; ld llersect(line const& other) { if(a==other.a)return -INF; return (ld)(other.b-b)/(a-other.a); } ld gety(ld x) { return a*x+b; } }; struct ConvexHull { deque<pair<line,double> >dq; void add(line l) { while(!dq.empty()&&l.llersect(dq.back().first)<=dq.back().second) { dq.pop_back(); } if(dq.empty())dq.push_back({l,-INF}); else dq.push_back({l,l.llersect(dq.back().first)}); } ld query(ld x) { while(dq.size()>1&&dq[1].second<=x)dq.pop_front(); //cout<<dq.front().first.a<<" "<<dq.front().first.b<<endl; return dq.front().first.gety(x); } }ch[MAXK]; ll n,k; ll pref[MAXN]; int main() { cin>>n>>k; for(ll i=1;i<=n;i++) { cin>>a[i]; pref[i]=pref[i-1]+a[i]; } for(ll i=0;i<=n;i++) { for(ll j=0;j<=k+1;j++)dp[i][j]=-INF; } for(ll i=1;i<=k+1;i++)ch[i].add({0,-INF}); ch[0].add({0,0}); for(ll i=1;i<=n;i++) { for(ll j=1;j<=k+1;j++) { dp[i][j]=ch[j-1].query(pref[i]); //cout<<i<<" "<<j<<" "<<dp[i][j]<<endl; } for(ll j=1;j<=k+1;j++) { ch[j].add({(ld)pref[i],(ld)dp[i][j]-pref[i]*pref[i]}); } } cout<<dp[n][k+1]<<endl; vector<ll>v; ll l=n; for(ll i=k;i>=1;i--) { for(ll l1=l-1;l1>=0;l1--) { if(dp[l1][i]+(pref[l]-pref[l1])*pref[l1]==dp[l][i+1]) { v.push_back(l1); l=l1; break; } } } for(auto xd:v)cout<<xd<<" "; cout<<endl; 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...