呃,不难,什么方程都在注释里面。
<span style="font-family:KaiTi_GB2312;font-size:18px;">#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 1001000 #define inf 0x3f3f3f3f /* f[i]=f[j]+sum[i]-sum[j]-p[j]*(x[i]-x[j])+c[i]; f[j]-sum[j]+p[j]*x[j]=x[i]*p[j]+f[i]-sum[i]-c[i] y=f[j]-sum[j]+p[j]*x[j] x=p[j] k=x[i] b=f[i]-sum[i]-c[i] */ using namespace std; int n,x[N],c[N]; long long p[N],sum[N],f[N]; struct Point { long long x,y; int id; Point(long long _x,long long _y,int _id):x(_x),y(_y),id(_id){} Point(){} }now,q[N]; int l,r; inline long long xmul(Point i,Point j,Point k){return (i.y-j.y)*(j.x-k.x)-(j.y-k.y)*(i.x-j.x);} int main() { // freopen("test.in","r",stdin); scanf("%d",&n); memset(f,0x3f,sizeof(f)); f[0]=0; //初始点经过计算为(0,0) for(int i=1;i<=n;i++) { scanf("%d%d%d",&x[i],&p[i],&c[i]); p[i]+=p[i-1]; sum[i]=sum[i-1]+p[i-1]*(x[i]-x[i-1]); int K=x[i]; while(l<r&&q[l].y-q[l+1].y>=(q[l].x-q[l+1].x)*K)l++; //斜率小于K就跳 int u=q[l].id; f[i]=f[u]+sum[i]-sum[u]-p[u]*(x[i]-x[u])+c[i]; now=Point(p[i],f[i]-sum[i]+p[i]*x[i],i); while(l<r&&xmul(now,q[r],q[r-1])<=0)r--; //斜(i,j)<=斜(j,k)就弹 q[++r]=now; } cout<<f[n]<<endl; return 0; } </span>