题意:将序列分成若干段,区间 l,r有代价,求最小代价。
显然有二维方程式
\(\large f_i=min\lbrace f_j+\sum_{k=j+1}^{i}p_i(x_k-x_i)\rbrace +c_i\)
\(\large f_i=min\lbrace f_j+\sum_{k=j+1}^i P_ix_k -\sum_{k=j+1}^i P_ix_i\rbrace+c_i\)
拆开中间的式子。
设\(T_i=\sum_{j=1}^{i}x_jp_j,P_i=\sum_{j=1}^{i}x_j\)
\(\large f_i=min\begin{equation} \left\{ \begin{array}{} f_j+x_i(P_i-P_j)+T_j-T_i \end{array}\right\}+c_i \end{equation}\)
\(f_j+x_i(P_i-P_j)+T_j-T_i\le f_k+x_i(P_i-P_k)+T_k-T_i\)
\(f_j+x_iP_j+T_j\le f_k+x_iP_k+T_k\)
\(f_j+T_j-(f_k+T_k)\le x_i(P_j-P_k)\)
\(\huge \frac{f_j+T_j -(f_k+T_k)}{(P_j-P_k)}< x_i\)
然后就愉快的维护一个下凸壳即可。
/*
@Date : 2019-07-31 13:54:47
@Author : Adscn (adscn@qq.com)
@Link : https://www.cnblogs.com/LLCSBlog
*/
#include<bits/stdc++.h>
using namespace std;
#define IL inline
#define RG register
#define gi getint()
#define gc getchar()
#define File(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
IL int getint()
{
RG int xi=0;
RG char ch=gc;
bool f=0;
while(ch<'0'||ch>'9')ch=='-'?f=1:f,ch=gc;
while(ch>='0'&&ch<='9')xi=(xi<<1)+(xi<<3)+ch-48,ch=gc;
return f?-xi:xi;
}
template<typename T>
IL void pi(T k,char ch=0)
{
if(k<0)k=-k,putchar('-');
if(k>=10)pi(k/10,0);
putchar(k%10+'0');
if(ch)putchar(ch);
}
#define int long long
const int N=1e6+7;
int _x[N],p[N],c[N];
int T[N],P[N];
int f[N];
inline double slope(int j,int k)
{
return 1.0*(f[j]+T[j]-f[k]-T[k])/(P[j]-P[k]);
}
signed main(void)
{
#ifndef ONLINE_JUDGE
// File("");
#endif
int n=gi;
for(int i=1;i<=n;++i)_x[i]=gi,p[i]=gi,c[i]=gi;
for(int i=1;i<=n;++i)
T[i]=T[i-1]+_x[i]*p[i],
P[i]=P[i-1]+p[i];
static int Q[N*2];
int l=1,r=1;
for(int i=1;i<=n;++i)
{
while(l<r&&slope(Q[l],Q[l+1])<_x[i])++l;
int j=Q[l];
f[i]=f[j]+_x[i]*(P[i]-P[j])+T[j]-T[i]+c[i];
while(l<r&&slope(Q[r],Q[r-1])>slope(i,Q[r]))--r;
Q[++r]=i;
}
cout<<f[n];
return 0;
}