# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
551564 | ToroTN | Split the sequence (APIO14_sequence) | C++14 | 636 ms | 87640 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define X first
#define Y second
struct line
{
ll m,c;
};
ll n,m,dp[100005][2],a[100005],qs[100005],u,g;
ll sum[100005],pos,cut;
int back[100005][205];
line t,now,pre;
stack<ll> stk;
vector<pair<line,ll> > v;
ll cal(line node,ll ptr)
{
return node.m*sum[ptr]+node.c;
}
ll intersect(line l1,line l2,line now)
{
return (now.c-l1.c)*(l2.m-now.m)-(now.c-l2.c)*(l1.m-now.m)<=0;
}
int main()
{
scanf("%lld%lld",&n,&m);
for(int i=1;i<=n;i++)scanf("%lld",&a[i]),sum[i]=sum[i-1]+a[i],qs[i]=qs[i-1]+a[i]*a[i];
for(int i=0;i<=n;i++)for(int j=0;j<=1;j++)dp[i][j]=2e18;
dp[0][0]=0;
memset(back,-1,sizeof back);
for(int i=1;i<=m+1;i++)
{
for(int j=0;j<=n;j++)dp[j][i%2]=2e18;
for(int j=0;j<=n;j++)
{
if(pos>=v.size())pos=v.size()-1;
while(pos+1<v.size()&&cal(v[pos+1].X,j)<=cal(v[pos].X,j))pos++;
if(v.size()>0)
{
dp[j][i%2]=cal(v[pos].X,j)+sum[j]*sum[j]-qs[j];
back[j][i]=v[pos].Y;
}
t={-2*sum[j],dp[j][1-(i%2)]+(sum[j]*sum[j]+qs[j])};
while(v.size()>=1)
{
now=v[v.size()-1].X;
if(now.m==t.m&&t.c<=now.c)
{
v.pop_back();
}else
{
break;
}
}
while(v.size()>=2)
{
now=v[v.size()-1].X;
pre=v[v.size()-2].X;
cut=intersect(now,pre,t);
if(cut)
{
v.pop_back();
}else
{
break;
}
}
v.pb({t,j});
}
while(!v.empty())v.pop_back();
}
u=n;
g=m+1;
while(1)
{
u=back[u][g];
--g;
if(u==0)break;
stk.push(u);
}
printf("%lld\n",(sum[n]*sum[n]-qs[n]-dp[n][(m+1)%2])/2);
while(!stk.empty())
{
printf("%lld ",stk.top());
stk.pop();
}
printf("\n");
}
/*
7 3
4 1 3 4 0 2 3
*/
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |