Submission #976741

#TimeUsernameProblemLanguageResultExecution timeMemory
976741tamir1Split the sequence (APIO14_sequence)C++17
33 / 100
198 ms131072 KiB
#include<bits/stdc++.h> #define ll long long using namespace std; ll n,k,i,j,l,a[100005],dp[205][100005],sum[100005],ans,dp2[205][100005],idx; vector<ll> v; struct line{ ll k,d,idx; }; ll val(line a,ll x){ return a.k*x+a.d; } double intt(line a,line b){ return double(b.d-a.d)/(a.k-b.k); } int main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin >> n >> k; for(i=1;i<=n;i++){ cin >> a[i]; sum[i]=sum[i-1]+a[i]; } for(l=1;l<=k;l++){ deque<line> q; q.push_back({sum[l-1],dp[l-1][l-1]-sum[n]*sum[l-1],l-1}); for(i=l;i<n;i++){ while(q.size()>=2 && val(q.back(),sum[i])<=val(q[q.size()-2],sum[i])) q.pop_back(); dp[l][i]=sum[n]*sum[i]-sum[i]*sum[i]+val(q.back(),sum[i]); dp2[l][i]=q.back().idx; line cur={sum[i],dp[l-1][i]-sum[n]*sum[i],i}; if(q[0].k==cur.k) q.pop_front(); while(q.size()>=2 && intt(cur,q[0])<=intt(q[0],q[1])) q.pop_front(); q.push_front(cur); } } for(i=k;i<n;i++){ if(dp[k][i]>=ans){ ans=dp[k][i]; idx=i; } } for(i=k;i>=1;i--){ v.push_back(idx); idx=dp2[i][idx]; } reverse(v.begin(),v.end()); cout << ans << "\n"; for(i=0;i<k;i++) cout << v[i] << " "; }
#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...