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>
#define db double
#define N 10002
using namespace std;
struct pt { db A,B; int vt; };
db giao(pt x,pt y)
{
return (y.B-x.B)/(x.A-y.A);
}
pt st[N];
long long g[N],f[N][202];
int i,j,n,sl,top,k,tr[N][202],d[N];
int main()
{
// freopen("ntu.inp","r",stdin);
// freopen("ntu.out","w",stdout);
cin>>n>>k; k++;
memset(f,-1,sizeof(f));
f[0][0]=0;
for(i=1;i<=n;i++) { cin>>g[i]; g[i]+=g[i-1]; }
for(sl=1;sl<=k;sl++)
{
top=0; j=1;
for(i=0;i<=n;i++)
{
j=min(j,top);
if(top>0)
{
while(j<top && st[j].A*g[i]+st[j].B<st[j+1].A*g[i]+st[j+1].B) j++;
f[i][sl]=st[j].A*g[i]+st[j].B;
tr[i][sl]=st[j].vt;
}
if(f[i][sl-1]>-1)
{
db A=g[i],B=f[i][sl-1]-g[i]*g[i];
while(top>=2 && giao({ A,B },st[top-1])>giao(st[top],st[top-1])) top--;
st[++top]={ A,B,i };
}
// cerr<<i<<" "<<sl-1<<" "<<top<<'\n';
// cerr<<f[i][sl]<<" "<<top<<" "<<i<<" "<<sl<<'\n';
}
}
cout<<f[n][k]<<'\n';
i=n;
while(k>0)
{
d[i]=1;
i=tr[i][k]; k--;
}
for(i=1;i<n;i++)
if(d[i]==1) cout<<i<<" ";
}
# | 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... |