# BZOJ1096_仓库建设_KEY

f[i]=min(f[j]+c[i]+(sum[i]-sum[j])*x[i]-(S[i]-S[j])};

f[j]+c[i]+sum[i]*x[i]-(S[i]-S[j])=x[i]*sum[j]+f[i]             y                   =k    x     +b

//红体字在y相减时会抵消，所以无影响。

code：

/**************************************************************
Problem: 1096
User: yekehe
Language: C++
Result: Accepted
Time:1100 ms
Memory:47796 kb
****************************************************************/

#include <cstdio>
#include <algorithm>
using namespace std;

typedef long long ll;

char tc()
{
static char tr[100000],*A=tr,*B=tr;
}

{
char c;while(c=tc(),c<'0'||c>'9');
int x=c-'0';while(c=tc(),c>='0'&&c<='9')x=x*10+c-'0';
return x;
}

const int MAXN=1000005;
ll N,x[MAXN],p,c[MAXN];
ll sum[MAXN],S[MAXN],f[MAXN];
ll l[MAXN],h,t;
double X(int i){return sum[i];}
double Y(int i){return f[i]+S[i];}
double get(int x,int y){return (Y(y)-Y(x))/(X(y)-X(x));}

int main()
{
register int i,j;
for(i=1;i<=N;i++){
sum[i]=sum[i-1]+p;
S[i]=S[i-1]+p*x[i];
}
h=t=0;
for(i=1;i<=N;i++){
while(h<t&&get(l[h],l[h+1])<x[i])h++;
j=l[h];f[i]=f[j]+c[i]+(sum[i]-sum[j])*x[i]-(S[i]-S[j]);
while(h<t&&get(l[t],l[t-1])>get(l[t],i))t--;
l[++t]=i;
}
printf("%lld",f[N]);
return 0;
}

posted @ 2018-03-12 10:15  Cptraser  阅读(75)  评论(0编辑  收藏