在工厂1和工厂3建立仓库,建立费用为10+10=20,运输费用为(9-5)*3 = 12,总费用32。如果仅在工厂3建立仓库,建立费用为10,运输费用为(9-0)*5+(9-5)*3=57,总费用67,不如前者优。
【数据规模】
对于100%的数据, N ≤1000000。 所有的Xi, Pi, Ci均在32位带符号整数以内,保证中间计算结果不超过64位带符号整数。
//設sum[i]=dis[i]*p[i] + dis[i-1]*p[i-1] + ... +dis[1]*p[1]
//psum[i]=p[i] + p[i-1] + ... +p[1]
//
//f[i]=f[j] + segma(dis[i]-dis[k])*p[k] + cost[i]; //j<i j<k<i
//f[i]=f[j] - (sum[i-1]-sum[j]) + dis[i]*(psum[i-1]-psum[j]) + cost[i];
//
//f[i]=-psum[j]*dis[i] + f[j]+sum[j] -sum[i-1]+dis[i]*psum[i-1]+cost[i]
//
//令dp[i]=f[i] a[j]=-psum[j] b[i]=dis[i] c[j]=f[j]+sum[j] d[i]=-sum[i-1] +dis[i]*psum[i-1] + cost[i]
//dp[i]=a[j]*b[i] +c[j] + d[i]
//a[j]*b[i] +dp[i]-d[i] == c[j]
//令a[j]=psum[j]=x,
// c[j]=f[j]+sum[j]=y
//x*b[i] + dp[i]-d[i] == y
//x*dis[i] + dp[i]-(-sum[i-1]+dis[i]*psum[i-1]+cost[i])
//最小化dp[i]->下凸包二分
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<string>
#include<queue>
using namespace std;
#ifdef WIN32
#define LL "%I64d"
#else
#define LL "%lld"
#endif
#define MAXN 1100000
#define MAXV MAXN*2
#define MAXE MAXV*2
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
typedef long long qword;
//設sum[i]=dis[i]*p[i] + dis[i-1]*p[i-1] + ... +dis[1]*p[1]
//psum[i]=p[i] + p[i-1] + ... +p[1]
//
//f[i]=f[j] + segma(dis[i]-dis[k])*p[k] + cost[i]; //j<i j<k<i
//f[i]=f[j] - (sum[i-1]-sum[j]) + dis[i]*(psum[i-1]-psum[j]) + cost[i];
//
//f[i]=-psum[j]*dis[i] + f[j]+sum[j] -sum[i-1]+dis[i]*psum[i-1]+cost[i]
//
//令dp[i]=f[i] a[j]=-psum[j] b[i]=dis[i] c[j]=f[j]+sum[j] d[i]=-sum[i-1] +dis[i]*psum[i-1] + cost[i]
//dp[i]=a[j]*b[i] +c[j] + d[i]
//a[j]*b[i] +dp[i]-d[i] == c[j]
//令a[j]=psum[j]=x,
// c[j]=f[j]+sum[j]=y
//x*b[i] + dp[i]-d[i] == y
//x*dis[i] + dp[i]-(-sum[i-1]+dis[i]*psum[i-1]+cost[i])
//最小化dp[i]->下凸包二分
inline int nextInt()
{
char ch;
int x=0;
bool flag=false;
do
ch=getchar(),flag=(ch==‘-‘)?true:flag;
while(ch<‘0‘||ch>‘9‘);
do x=x*10+ch-‘0‘;
while (ch=getchar(),ch<=‘9‘ && ch>=‘0‘);
return x*(flag?-1:1);
}
struct point
{
qword x,y;
}seq[MAXN];
int tops=-1;
qword xmul(point p1,point p2,point p3)
{
return (p2.x-p1.x)*(p3.y-p1.y) - (p2.y-p1.y)*(p3.x-p1.x);
}
double get_v(point p1,point p2)
{
return (double)(p2.y-p1.y)/(p2.x-p1.x);
}
qword dis[MAXN],cost[MAXN],p[MAXN];
qword psum[MAXN],sum[MAXN];
qword f[MAXN];
int n;
void work3()
{
int i,j;
int l,r,mid;
memset(f,0x3f,sizeof(f));
f[0]=0;
seq[0].x=psum[0];
seq[0].y=f[0]+sum[0];
tops=0;
point pt;
for (i=1;i<=n;i++)
{
l=-1,r=tops;
while (l+1<r)
{
mid=(l+r)>>1;
if (get_v(seq[mid],seq[mid+1])>dis[i])
{
r=mid;
}else
{
l=mid;
}
}
//x*dis[i] + dp[i]-(-sum[i-1]+dis[i]*psum[i-1])+cost[i]=y
//dp[i]=y-x*dis[i]+(-sum[i-1]+dis[i]*psum[i-1])-cost[i]
//
//dp[i]=f[j]+sum[j]-psum[j]*dis[i]+(-sum[i-1]+dis[i]*psum[i-1]-cost[i])
f[i]=seq[r].y-seq[r].x*dis[i]+(-sum[i-1]+dis[i]*psum[i-1])+cost[i];
pt.x=psum[i];
pt.y=f[i]+sum[i];
while (tops>0 && xmul(seq[tops-1],seq[tops],pt)<=0)tops--;
seq[++tops]=pt;
}
//for (i=0;i<=n;i++)
//3 cout<<f[i]<<endl;
cout<<f[n]<<endl;
}
void work1()
{
int i,j,k;
memset(f,0x3f,sizeof(f));
f[0]=0;
qword t;
for (i=1;i<=n;i++)
{
for (j=0;j<i;j++)
{
t=0;
for (k=j+1;k<i;k++)
{
t+=(dis[i]-dis[k])*p[k];
}
f[i]=min(f[i],f[j]+t+cost[i]);
}
}
// for (i=0;i<=n;i++)
// cout<<f[i]<<endl;
cout<<f[n]<<endl;
}
void work2()
{
int i,j,k;
memset(f,0x3f,sizeof(f));
f[0]=0;
for (i=1;i<=n;i++)
{
for (j=0;j<i;j++)
{
f[i]=min(f[i],-psum[j]*dis[i]+f[j]+sum[j] -sum[i-1]+dis[i]*psum[i-1]+cost[i]);
}
}
//for (i=0;i<=n;i++)
// cout<<f[i]<<endl;
cout<<f[n]<<endl;
}
int main()
{
freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
int i,j,k;
int x,y,z;
scanf("%d",&n);
for (i=1;i<=n;i++)
{
dis[i]=nextInt();
p[i]=nextInt();
cost[i]=nextInt();
// if (!p[i])
// {
// i--;n--;
// }
}
for (i=1;i<=n;i++)
{
psum[i]=psum[i-1]+p[i];
sum[i]=sum[i-1]+p[i]*dis[i];
}
//work1();
//work2();
work3();
return 0;
}