很多斜率优化的论文里都有这道题.

//By YY_More
#include<cstdio>
struct point{
	long long x,y;
} now,D[1000010];
int N,X[1000010],C,L,R;
long long sumP[1000010],sumXP[1000010],W[1000010];
inline long long xmul(point a,point b,point c){
	return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}	
int main(){
	scanf("%d",&N);
	for (int i=1;i<=N;i++){
		scanf("%d%d%d",&X[i],&sumP[i],&C);
		sumXP[i]=sumXP[i-1]+X[i]*sumP[i];
		sumP[i]+=sumP[i-1];
		W[i]=C+X[i]*sumP[i]-sumXP[i];
	}
	for (int i=1;i<=N;i++){
		while (L<R&&D[L].y-D[L].x*X[i]>=D[L+1].y-D[L+1].x*X[i]) L++;
		now.x=sumP[i];now.y=sumXP[i]+D[L].y-D[L].x*X[i]+W[i];
		while (L<R&&xmul(D[R-1],D[R],now)<=0) R--;
		D[++R]=now;
	}
	printf("%I64d\n",D[R].y-sumXP[N]);
	return 0;
}

相关文章:

  • 2021-06-05
  • 2021-11-16
  • 2021-09-04
  • 2022-01-16
  • 2021-11-20
  • 2021-08-26
  • 2021-12-12
  • 2021-07-04
猜你喜欢
  • 2021-11-23
  • 2022-03-01
  • 2021-10-04
  • 2021-08-27
  • 2021-06-15
  • 2021-06-24
相关资源
相似解决方案