Rokua P3049 Landscaping (Greedy | | DP)

Source: Internet
Author: User

Test Instructions: There are n blocks of land, each with a[i] soil, which is now transformed into b[i] soil, there are 3 kinds of operations: (1) The cost of X to any land to increase 1 soil, (2) to spend Y to any land to reduce 1 soil; (3) Cost z*|i-j| 1 of the soil I was transported to the land J. Ask what the minimum cost is.

Analysis:

Referring to the ideas given by the great Gods of the valley, the following is a brief description

The simple saying is that for each point, it takes a certain amount of value to make its quantity

into b[i] soil, but this cost is not necessarily optimal, can be adjusted later

To achieve a smaller cost, the adjustment of course is the lack of change before and after

Change before and after adding these two kinds of land to consider, for the lack of land can be considered

To buy land directly at the cost of X, you can also consider adding soil from previous changes

To spend Z transfer over. The same is true for changes in land that have been increased before and after

Define OPT (x) to represent the temporary optimal value of the current point, which is the minimum cost

For example, if the current first point is missing before and after the change, it is not enumerated to

Change the point before and after, so you can only change it by spending the X scheme to make it equal

Opt (first_point) = (B[first_point]-a[first_point]) *x ==> Of course, it's only temporary.

For (by subscript from small to large enumerate each point):

If the land difference is not changed before and after the current change continue

If the land is missing before and after the current change:

Suppose the current point is J, and previously there were I1, I2, i3 ... is a change in the pre-and post

If you choose to transfer from these I to J make the changes equal before and after

There are (J-I1) *z-opt (I1), (J-I2) *z-opt (I2), (J-I3) *z-opt (i3) ... Of course, take the minimum from these.

Consider why it is like, because it is transferred from I, so the optimal cost before I, we should be to lose

In order to offset the cost of the previous I, so that it transferred to J here, to produce a better plan, do not understand to look down

Simplify the J*z-max[i + opt (i)] ==> from so many I take the largest, to ensure that the cost is minimal

So the last opt (j) = min (X, j*z-max[i + opt (i)]) ==> of course, and direct purchase of this scheme to take a min

If the land is increased before and after the change:

Like the lack of a situation, it doesn't elaborate.

Ans + = opt (j)

The above is the pseudo-code idea This greedy is obviously correct, constantly from a variety of ways to take the best

This greedy idea is easy to think of, just don't know how to write with the code, or not a clear idea

This first determine a value, and then to adjust to better greedy ideas, learning

As for DP's idea, actually I have not seen, have time to say

#include <bits/stdc++.h>#defineLL Long Longusing namespacestd; LL N, X, Y, Z;priority_queue<LL> Opt_lack;///represents the optimal value for each missing pointPriority_queue<ll> Opt_surplus;///represents the optimal value for each extra pointintMainvoid){     while(~SCANF ("%lld%lld%lld%lld", &n, &x, &y, &Z)) {         while(!opt_lack.empty ()) Opt_lack.pop ();  while(!opt_surplus.empty ()) Opt_surplus.pop (); LL before, after, ans=0;  for(LL j=1; j<=n; J + +) {scanf ("%lld%lld", &before, &After ); if(before = = after)Continue; if(Before <After ) {                 for(LL i=1; i<=after-before; i++){                    if(Opt_surplus.empty () | | j*z-opt_surplus.top () >=X) {ans+=X; Opt_lack.push (J*z+X); }Else{LL T=opt_surplus.top (); Opt_surplus.pop (); Ans+ = J*z-T; Opt_lack.push (J*z + j*z-T); }                }            }Else{                 for(LL i=1; i<=before-after; i++){                    if(Opt_lack.empty () | | j*z-opt_lack.top () >=Y) {ans+=Y; Opt_surplus.push (J*z+Y); }Else{LL T=opt_lack.top (); Opt_lack.pop (); Ans+ = J*z-T; Opt_surplus.push (J*z + j*z-T); } }}} printf ("%lld\n", ans); }    return 0;}
View Code

Rokua P3049 Landscaping (Greedy | | DP)

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.