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 long long
#define N 100002
using namespace std;
struct pt { db A,B; int vt; };
struct ps { long long tu,mau; };
ps giao(pt x,pt y)
{
long long tu=y.B-x.B,mau=x.A-y.A;
// if(tu==0 && mau==0) cerr<<1<<'\n';
if(mau<0) { tu=-tu; mau=-mau; }
return { tu,mau };
}
bool operator < (ps a,ps b)
{
if(a.tu<0)
{
if(b.tu>=0) return 1;
a.tu=-a.tu; b.tu=-b.tu;
//return ;
swap(a,b);
}
if(b.tu<0)
{
if(a.tu>=0) return 0;
a.tu=-a.tu; b.tu=-b.tu;
swap(a,b);
// return b<a;
}
return a.tu*b.mau<a.mau*b.tu;
}
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++;
for(i=0;i<=n;i++)
for(j=0;j<=k;j++) f[i][j]=-1;
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;
// int ch=sl%2;
for(i=0;i<=n;i++)
{
if(top>0)
{
j=min(j,top);
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(sl==2 && i==n) cerr<<top<<" "<<st[j].vt<<'\n';
}
if(f[i][sl-1]>-1)
{
db A=g[i],B=f[i][sl-1]-g[i]*g[i];
while(top>0 && A==st[top].A) top--;
while(top>=2 && giao({ A,B,i },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';*/
// for(j=0;j<i;j++)
// if(f[j][sl-1]>-1) f[i][sl]=max(f[i][sl],f[j][sl-1]+(g[i]-g[j])*g[j]);*/
}
}
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<<" ";
cout<<'\n';
}
# | 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... |