Two sawmills)

Source: Internet
Author: User
Tags bitset

Http://main.edu.pl/en/archive/ceoi/2004/two


Slope-optimized DP should be said to be the first slope-optimized DP. When the formula is pushed, various pitfalls are still referred to the idea of hzq shiniu. The details are slightly different,

For the sake of convenience of thinking, I first flipped the given sequence, that is, turning the order of points from the top of the hill to the bottom of the hill to the top of the mountain. The number starts from 1, the first point is the lowest-altitude logging farm, so there are n + 1 points. W [I] indicates the weight of the I point, dist [I] indicates the distance between the I-th vertex and the first vertex. DP [I] indicates the optimal solution for building the second logging farm to the I-th vertex.

Then there is

DP [I] = min (s [1, J-1] + s [J, I-1] + s [I, n + 1]) 1

S [L, R] = Σ W [I] * (Dist [I]-Dist [I]) (L <= I <= r) 2

Set SW [I] = Σ W [J] (1 <= j <= I), 3

SWD [I] = Σ W [J] * Dist [J] (1 <= j <= I) 4

S [L, R] = SWD [R]-SWD [L-1]-(SW [R]-SW L-1]) * Dist [l];

Simplified and available after 1

DP [I] = min (SWD [n + 1]-SWD [0]-(SW [n + 1]-SW [I-1]) * Dist [I]-(SW [I-1]-SW [J-1]) * Dist [J]-(SW [J-1]-SW [0]) * Dist [1])

Since Dist [1] is equal to 0

DP [I] = min (-(SW [I-1]-SW [J-1]) * Dist [J]) + SWD [n + 1]-SWD [0]-(SW [n + 1]-SW [I-1]) * Dist [I]

Think that the latter part is a constant unrelated to J, so it does not affect decision-making, and can be ignored.

So to solve the problem, only min (-SW [I-1]) * Dist [J] + SW [J-1] * Dist [J]) is left.

Since the-SW [I-1] can be regarded as a constant in the I-Round Decision-making, X is used to represent Dist [J], Y is used to represent SW [J-1] * Dist [J],

Then the optimization target G = min (-ax + Y), set X *, y * as the optimal solution (note that the optimal solution is only determined by J)

If G =-Ax * + y * exists, y * = AX * + G can be obtained after the item is moved. Because a> 0, this is equivalent to moving a known straight line up until it is switched to any previous point (x, y). At this time, G is the optimal solution, it is not difficult to find that the optimal decision point forms a convex line, and because a increases monotonically, and the slope of the convex line increases monotonically, it can discard all the points before the current optimal decision, these points cannot be the greatest advantage of subsequent decision-making.

# Include <iostream> # include <cstdio> # include <cstdlib> # include <cmath> # include <queue> # include <algorithm> # include <vector> # include <cstring> # include <stack> # include <cctype> # include <utility> # include <map> # include <string> # include <climits> # include <set> # include <string> # include <sstream> # include <utility> # include <ctime> # include <bitset> Using STD:: priority_queue; Using STD: vector; Using STD :: Swap; Using STD: Stack; Using STD: sort; Using STD: Max; Using STD: min; Using STD: pair; Using STD: map; using STD: string; Using STD: CIN; Using STD: cout; Using STD: Set; Using STD: queue; Using STD: string; Using STD :: stringstream; Using STD: make_pair; Using STD: Getline; Using STD: greater; Using STD: Endl; Using STD: multimap; Using STD: deque; using STD: unique; Using STD: lower_bound; Using STD: random_shuffle; Using STD: bitset; Using STD: upper_bound; Using STD: Multiset; typedef long ll; typedef unsigned long ull; typedef pair <int, int> pair; typedef multimap <int, int> MMAP; typedef ll ty; typedef long double lf; const int maxn (20010); const int maxm (100010); const int MaxE (100010 ); const int maxk (6); const int hsize (31313); const int sigma_size (26); const int maxh (19); const int INFI (INT_MAX-1)> 1 ); con St ull base (31); const ll Lim (10000000); const int inv (-10000); const int Mod (20100403); const double EPS (1e-7 ); const lf Pi (ACOs (-1.0); Template <typename T> void checkmax (T & A, t B) {If (B> A) A = B ;} template <typename T> void checkmin (T & A, t B) {If (B <A) A = B;} template <typename T> T ABS (const T &) {return a <0? -A: A;} ll X [maxn], Y [maxn]; int que [maxn]; int front, back; ll Dist [maxn], SW [maxn]; int main () {int N; while (~ Scanf ("% d", & N) {+ + N; // a total of N + 1 points, subscript 1 from ll SWD = 0; for (INT I = N; I> = 2; -- I) scanf ("% i64d % i64d", SW + I, DIST + I); For (INT I = 1; I <= N; ++ I) {Dist [I] + = DIST [I-1]; SWD + = DIST [I] * SW [I]; SW [I] + = Sw [I-1];} For (INT I = 1; I <= N; + I) {x [I] = DIST [I]; Y [I] = Sw [I-1] * Dist [I];} ll ans = 1e11; front = 0; back =-1; que [+ back] = 1; que [+ back] = 2; for (INT I = 3; I <= N; ++ I) {While (back-front> 0 & (-SW [I-1]) * X [que [Front + 1] + Y [que [Front + 1] <= (-SW [I-1]) * (X [que [Front]) + Y [que [Front]) + + front; checkmin (ANS, SWD-(SW [N]-SW [I-1]) * Dist [I] + (-SW [I-1]) * (X [que [Front]) + Y [que [Front]); while (back-front> 0 & (Y [I]-y [que [back-1]) * (X [I]-X [que [Back])> = (Y [I]-y [que [Back]) * (X [I]-X [que [back-1]) -- back; que [++ back] = I;} printf ("% i64d \ n ", ans);} return 0 ;}




Related Article

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.