Bzoj 1096: [ZJOI2007] Warehouse Construction (DP + slope optimization)

Source: Internet
Author: User

DP (v) = min (DP (P) +cost (P,V)) +c (v)

set sum (v) =∑pi (1≤i≤v), CNT (v) =∑pi*xi (1≤i≤v),

Then cost (P,V) = x (v) * (sum (v)-sum (P))-(CNT (v)-cnt (p))

Assuming DP (v) is transferred by DP (i) Transfer ratio DP (j), I>j

so DP (i) +cost (I,V) < DP (j) +cost (j,v)

i.e. DP (i) +x (v) * (sum (v)-sum (i))-(CNT (v)-cnt (i)) < DP (j) + x (v) * (sum (v)-sum (j))-(CNT (v)-cnt (j) )

Set F (x) = DP (x) +cnt (x), simplify (f (i)-f (j))/(sum (i)-sum (j)) < X (v)

Then on the slope optimization, the monotone queue maintains a convex function

-----------------------------------------------------------------------

#include <bits/stdc++.h>using namespace std;typedef long Long ll;const int MAXN = 1000009;ll SUM[MAXN], CNT[MAXN], DP[MAXN];int X[MAXN], N, Q[MAXN];inline double slope (int a, int b) {return (double) (Dp[b] + cnt[b]-dp[a]-cnt[a])/(Sum[b]-sum[a]);}int main () {scanf ("%d", &n);dp[0] = cnt[0] = sum[0] = 0;for (int i = 1; I <= N; i++) {int p; scanf ("%d%d%lld", X + I, &p, DP + i);Sum[i] = sum[i-1] + p;Cnt[i] = Cnt[i-1] + LL (p) * X[i];}int QH = 0, qt = 1; Q[0] = 0; Q [QH, QT]for (int i = 1; I <= N; i++) {while (Qt-qh > 1 && slope (Q[QH], Q[QH + 1]) < x[i]) qh++;int best = Q[QH];Dp[i] + = Dp[best] + ll (X[i]) * (Sum[i]-sum[best])-cnt[i] + cnt[best];While (Qt-qh > 1 && slope (q[qt-2], q[qt-1]) >= slope (q[qt-1], i)) qt--;q[qt++] = i;}printf ("%lld\n", Dp[n]);return 0;}

-----------------------------------------------------------------------

1096: [ZJOI2007] Warehouse Construction Time Limit:Ten Sec Memory Limit:162 MB
Submit:3001 Solved:1286
[Submit] [Status] [Discuss] Description

L The company has n factories, which are spread from high to the bottom of a mountain. , factory 1 at the summit, Factory N at the foot of the hill. As the mountain is in the inland area of the plateau (dry and less rain), l companies generally put products directly in the open air to save costs. Suddenly one day, the president of L company Mr. L received a telephone call from the meteorological department, and was told that there would be a rainstorm after three days, so Mr. L decided to urgently set up some warehouses in some factories to prevent the products from getting badly soaked. Depending on the terrain, the cost of building warehouses in different factories may be different. The first plant currently has a finished pi, and the cost of establishing a warehouse at the location of the I plant is CI. For a factory that does not have a warehouse, its products should be transported to other warehouses for storage, and since the external sales department of the company's products are located at the foot of the factory N, the product can only be transported to the mountain (that is, only to the warehouse of the larger number of factories), of course, shipping products are also cost- Suppose the cost of transporting 1 units of a product is 1. Assume that the built-in warehouse capacity is large enough to accommodate all the products. You will get the following data:? Factory I distance from Plant 1 XI (where x1=0);? Factory I currently has a quantity of finished products pi; In the factory I set up the cost of the warehouse CI; Please help L company to find a warehouse construction plan, make the total cost (construction cost + transportation cost) the smallest.

Input

The first line contains an integer n, which indicates the number of factories. The next n rows each row contains two integers in the XI, Pi, Ci, meaning title described.

Output

Contains only one integer, which is the cost to find the best solution.

Sample Input 3
0 5 10
5 3 100
9 6Sample Output +HINT

In the factory 1 and plant 3 to establish warehouses, the establishment of the cost of 10+10=20, transport costs (9-5) * * = 12, the total cost of 32. If the warehouse is established only at factory 3, the establishment cost is 10, the transportation cost is (9-0) *5+ (9-5) *3=57, the total cost 67, the former excellent.

"Data Size"

For 100% of data, n≤1000000. All XI, Pi, and CI are within a 32-bit signed integer, guaranteeing that the intermediate result does not exceed the 64-bit signed integer.

Source

Bzoj 1096: [ZJOI2007] Warehouse Construction (DP + slope optimization)

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.