rt,这是《深进》中的代码,交上去 0 分。
#include<bits/stdc++.h>
using namespace std;
const int maxn=100010;
typedef long long ll;
ll f[2][maxn],pre[210][maxn];
int h,t,n,k,a[maxn];
struct vec{
int id,x;
ll y;
ll operator()(int v){
return x*v+y;
}
}q[maxn];
ll cross(vec a,vec b,vec c){
a.x-=b.x,a.y-=b.y,b.x-=c.x,b.y-=c.y;
return b.x*a.y-a.x*b.y;
}
int main(){
cin>>n>>k;
for(int i=1;i<=n;++i){
cin>>a[i],a[i]+=a[i-1];
}
for(int T=1;T<=k;++T){
h=t=0;
q[0]=(vec){0,0,0};
for(int i=1;i<=n;++i){
while(h<t&&q[h](a[i])<=q[h+1](a[i]))++h;
f[T&1][i]=q[h](a[i]);
pre[T][i]=q[h].id;
vec x=(vec){i,a[i],f[T&1^1][i]-(ll)a[i]*a[i]};
while(h<t&&cross(q[t-1],q[t],x)<=0)--t;
q[++t]=x;
}
}
cout<<f[k&1][n]<<endl;
int now=n;
for(int i=k;i>=1;--i){
cout<<pre[i][now]<<" ";
now=pre[i][now];
}
}