Bzoj_1096_[zjoi2007]_ Warehouse Construction _ (Slope optimization Dynamic programming + monotone queue + special prefixes and tricks)

Source: Internet
Author: User

Describe

http://www.lydsy.com/JudgeOnline/problem.php?id=1096

Have \ (n\) factory, give the factory of the first \ (i\) to plant 1th distance \ (x[i]\), the quantity of goods (p[i]\), the cost of building the warehouse (c[i]\).

Now to load all the goods into the warehouse, the goods of the I\ factory can choose to build the warehouse and deposit in the i\, or move to the (k\) warehouse ((i<k<=n) \). The cost of moving is the product of quantity and distance.

Analysis

Let's think about the DP equation.

Use \ (dp[i]\) to represent the first \ (I\) factory, and the minimum cost of building a warehouse in \ (i\). Then there is the transfer equation: $ $DP [I]=min\{dp[j]+cost (j+1,i) \}+c[i]$$

where \ (cost (j+1,i) \) represents the total costs of shipping the goods (j+1\) to \ (i\) to the \ (i\) number.

How does this \ (cost\) beg?

We use the prefix and \ (s1[i]\) to represent \ (p[1]+p[2]+...+p[i]\).

This means that \ ((S1[i]-s1[j]) \times{x[i]}) represents the total cost of transporting all goods from \ (j+1\) to \ (i\) from number 1th to the \ (i\) number.

We found that for each \ (k (j+1<=k<=i) \) factory, the cost of transporting \ (p[k]\) goods from No. 1th to the \ (j\) was spent. We are now going to subtract these extra expenses.

denoted by a prefix and \ (s2[i]\) \ (p[1]x[1]+p[2][x2]+...+p[i]x[i]\).

So \ (s2[i]-s2[j]\) is the excess of those costs.

However, the complexity of the algorithm is \ (o (n^2) \), destined to explode, so we consider using slope optimization method to reduce the complexity of the (O (n) \).

First variant, primitive $ $DP [i]=min{dp[j]+ (S1[i]-s1[j]) \times{x[i]}-(S2[i]-s2[j])}+c[i]$$

Transform to $ $DP [i]=min{dp[j]+s2[j]-x[i]\times{s1[j]}}+s1[i]\times{x[i]}-s2[i]+c[i]$$

Set \ (j<k<i\), \ (k\) ratio \ (j\) decision is better. There is $ $DP [k]+s2[k]-x[i]\times{s1[k]}<dp[j]+s2[j]-x[i]\times{s1[j]}$$

That is $$\frac{(Dp[k]+s2[k])-(dp[j]+s2[j])}{s1[k]-s1[j]}<x[i]$$

Monotonous queue to make a mess ...

1#include <bits/stdc++.h>2 using namespacestd;3 4typedefLong Longll;5 Const intmaxn=1e6+5;6 ll N;7 ll X[MAXN],P[MAXN],C[MAXN],S1[MAXN],S2[MAXN],Q[MAXN],DP[MAXN];8InlinevoidRead (ll &x) {x=0; ll k=1;CharC for(C=getchar ();c<'0'|| C>'9'; C=getchar ())if(c=='-') k=-1; for(; c>='0'&&c<='9'; C=getchar ()) x=x*Ten+c-'0'; x*=K;}9inline ll up (intKintj) {returndp[k]-dp[j]+s2[k]-s2[j];}TenInline ll DN (intKintj) {returns1[k]-s1[j];} One voidinit () { A read (n); -      for(intI=1; i<=n;i++){ - read (X[i]), read (P[i]), read (C[i]); thes1[i]=s1[i-1]+P[i]; -s2[i]=s2[i-1]+p[i]*X[i]; -     } - } + voidsolve () { -ll front=0, tail=1; +      for(intI=1; i<=n;i++){ A          while(front+1<tail&&up (q[front+1],q[front]) <dn (q[front+1],q[front] *x[i]) front++; at         intJ=q[front]; dp[i]=dp[j]+ (S1[i]-s1[j]) *x[i]-(S2[i]-s2[j]) +C[i]; -          while(front+1<tail&&up (i,q[tail-1]) *dn (q[tail-1],q[tail-2]) <=up (q[tail-1],q[tail-2]) *dn (i,q[tail-1])) tail--; -q[tail++]=i; -     } -printf"%lld\n", Dp[n]); - } in intMain () { - init (); to solve (); +     return 0; -}
View Code

Bzoj_1096_[zjoi2007]_ Warehouse Construction _ (Slope optimization Dynamic programming + monotone queue + special prefixes and tricks)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.