1096: [ZJOI2007]仓库建设php
Time Limit: 20 Secios
Memory Limit: 256 MB优化
题目链接spa
http://www.lydsy.com/JudgeOnline/problem.php?id=1096code
Descriptionorm
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公司寻找一个仓库建设的方案,使得总的费用(建造费用+运输费用)最小。blog
Inputip
Outputci
仅包含一个整数,为能够找到最优方案的费用。get
Sample Input
3
0 5 10
5 3 100
9 6 10
Sample Output
32
HINT
题意
题解:
n^2的dp很显然就推出来了
而后斜率优化DP就吼了……
代码:
#include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) #define maxn 1000100 #define mod 10007 #define eps 1e-9 int Num; //const int inf=0x7fffffff; //нчоч╢С const int inf=0x3f3f3f3f; inline ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } //************************************************************************************** ll p[maxn],s[maxn],c[maxn]; ll h[maxn]; ll t,w; ll f[maxn],q[maxn]; int n; ll slope(int k,int j) { return double(f[j]-f[k]+h[j]-h[k])/double(p[j]-p[k]); } int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%lld%lld%lld",&s[i],&p[i],&c[i]); for(int i=1;i<=n;i++) { h[i]=h[i-1]+s[i]*p[i]; p[i]=p[i-1]+p[i]; } for(int i=1;i<=n;i++) { while(t<w&&slope(q[t],q[t+1])<s[i])t++; int j = q[t]; f[i]=f[j]+s[i]*(p[i]-p[j])-h[i]+h[j]+c[i]; while(t<w&&slope(q[w-1],q[w])>slope(q[w],i))w--; q[++w]=i; } printf("%lld\n",f[n]); }