1096: [ZJOI2007]仓库建设
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3333 Solved: 1430
[ Submit][ Status][ Discuss]
Description
L公司有N个工厂,由高到底分布在一座山上。如图所示,工厂1在山顶,工厂N在山脚。 由于这座山处于高原内陆地区(干燥少雨),L公司一般把产品直接堆放在露天,以节省费用。突然有一天,L公司的总裁L先生接到气象部门的电话,被告知三天之后将有一场暴雨,于是L先生决定紧急在某些工厂建立一些仓库以免产品被淋坏。由于地形的不同,在不同工厂建立仓库的费用可能是不同的。第i个工厂目前已有成品Pi件,在第i个工厂位置建立仓库的费用是Ci。对于没有建立仓库的工厂,其产品应被运往其他的仓库进行储藏,而由于L公司产品的对外销售处设置在山脚的工厂N,故产品只能往山下运(即只能运往编号更大的工厂的仓库),当然运送产品也是需要费用的,假设一件产品运送1个单位距离的费用是1。假设建立的仓库容量都都是足够大的,可以容下所有的产品。你将得到以下数据: 工厂i距离工厂1的距离Xi(其中X1=0); 工厂i目前已有成品数量Pi; 在工厂i建立仓库的费用Ci; 请你帮助L公司寻找一个仓库建设的方案,使得总的费用(建造费用+运输费用)最小。
Input
第一行包含一个整数N,表示工厂的个数。接下来N行每行包含两个整数Xi, Pi, Ci, 意义如题中所述。
Output
仅包含一个整数,为可以找到最优方案的费用。
Sample Input
0 5 10
5 3 100
9 6 10
Sample Output
HINT
在工厂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位带符号整数。
Source
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;
typedef double DB;
typedef long long LL;
const int maxn = 1E6 + 10;
LL d[maxn],sp[maxn],sum[maxn],f[maxn],c[maxn];
int n,q[maxn],tail,head;
LL read()
{
LL ret = 0;
char ch = getchar();
while (ch < '0' || ch > '9') ch = getchar();
while (ch >= '0' && ch <= '9') {
ret = ret*10 + ch - '0';
ch = getchar();
}
return ret;
}
DB K(int x,int y)
{
return (DB)(f[y]-f[x]+sum[y]-sum[x])/(DB)(sp[y]-sp[x]);
}
int main()
{
#ifdef YZY
freopen("yzy.txt","r",stdin);
#endif
cin >> n;
for (int i = 1; i <= n; i++) {
d[i] = read();
sp[i] = read();
c[i] = read();
sum[i] = d[i]*sp[i] + sum[i-1];
sp[i] += sp[i-1];
}
f[0] = 0; f[1] = c[1];
head = tail = 1; q[1] = 0;
for (int i = 2; i <= n; i++) {
while (tail - head >= 1 && K(q[tail-1],q[tail]) > K(q[tail],i-1))
--tail;
q[++tail] = i-1;
while (tail - head >= 1 && K(q[head],q[head+1]) < (DB)(d[i]))
++head;
int x = q[head];
f[i] = f[x] + d[i]*(sp[i-1]-sp[x]) - sum[i-1] + sum[x] + c[i];
}
cout << f[n];
return 0;
}