Submission #242917

#TimeUsernameProblemLanguageResultExecution timeMemory
242917uacoder123Split the sequence (APIO14_sequence)C++14
0 / 100
396 ms131076 KiB
#include <bits/stdc++.h> using namespace std; #define F first #define S second #define FOR(i,a,b) for (auto i = (a); i <= (b); ++i) #define NFOR(i,a,b) for(auto i = (a); i >= (b); --i) #define all(x) (x).begin(), (x).end() #define sz(x) int(x.size()) #define mp(i,a) make_pair(i,a) #define pb(a) push_back(a) #define bit(x,b) (x&(1LL<<b)) typedef long long int lli; typedef pair <lli,lli> ii; typedef pair <lli,ii> iii; typedef vector <lli> vi; lli p[100001],arr[100001],p1[100001],dp[202][100002],kval[202][100002]; void cal(lli g,lli s,lli e,lli sl,lli se) { lli mid=(s+e)/2,k=sl; for(lli i=sl;i<=se;++i) { if(i>=mid&&dp[g][mid]>(((p[i]-p[mid-1])*(p[i]-p[mid-1])-(p1[i]-p1[mid-1]))/2)+dp[g-1][i+1]) { dp[g][mid]=(((p[i]-p[mid-1])*(p[i]-p[mid-1])-(p1[i]-p1[mid-1]))/2)+dp[g-1][i+1]; k=i; } } kval[g][mid]=k; if(s!=e) { if(mid-1>=s) cal(g,s,mid-1,sl,k); if(mid+1<=e) cal(g,mid+1,e,k,se); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); lli test=1; for(;test>0;--test) { lli n,k; cin>>n>>k; k++; p[0]=0; p1[0]=0; for(lli i=1;i<=n;++i) { cin>>arr[i]; p[i]=p[i-1]+arr[i]; p1[i]=p1[i-1]+(arr[i]*arr[i]); } for(lli i=1;i<=n;++i) { dp[1][i]=(p[n]-p[i-1])*(p[n]-p[i-1])-(p1[n]-p1[i-1]); dp[1][i]/=2; kval[1][i]=n; } for(lli i=2;i<=k;++i) { for(lli j=0;j<=n+1;++j) dp[i][j]=1000000000000000000; cal(i,1,n,1,n); } cout<<((p[n]*p[n]-p1[n])/2)-dp[k][1]<<endl; lli now=1; while(k!=1) { cout<<kval[k][now]<<' '; now=kval[k][now]+1; k--; } 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...