Submission #242924

#TimeUsernameProblemLanguageResultExecution timeMemory
242924uacoder123Split the sequence (APIO14_sequence)C++14
100 / 100
1595 ms82444 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[100002],p1[100002],dp[2][100002]; int kval[202][100002]; void cal(int g,int s,int e,int sl,int se) { lli mid=(s+e)/2,k=sl; for(lli i=sl;i<=se;++i) { if(i>=mid&&dp[1][mid]>(((p[i]-p[mid-1])*(p[i]-p[mid-1])-(p1[i]-p1[mid-1]))/2)+dp[1-1][i+1]) { dp[1][mid]=(((p[i]-p[mid-1])*(p[i]-p[mid-1])-(p1[i]-p1[mid-1]))/2)+dp[1-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,f; cin>>n>>k; k++; p[0]=0; p1[0]=0; for(lli i=1;i<=n;++i) { cin>>f; p[i]=p[i-1]+f; p1[i]=p1[i-1]+(f*f); } for(lli i=1;i<=n;++i) { dp[0][i]=(p[n]-p[i-1])*(p[n]-p[i-1])-(p1[n]-p1[i-1]); dp[0][i]/=2; kval[1][i]=n; } for(lli i=2;i<=k;++i) { for(int j=0;j<=100001;++j) dp[1][j]=150000000000000000; cal(i,1,n-i+1,1,n); swap(dp[0],dp[1]); } cout<<((p[n]*p[n]-p1[n])/2)-dp[0][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...