"BZOJ1911" "Apio2010" Special Ops team, slope optimization dp topless (slope monotonic)

Source: Internet
Author: User

Exercises

First the bare DP equation is:

F (x) =a*x*x+b*x+c; Sum is prefixed and

F[i]=f[j]+f (Sum[i]-sum[j]);


Then open the side with J, with the side of I, to form a y=kx+b, so that Y and X are containing J i,k and B contain I without J.

Get: f[j]+asum[j]*sum[j]-b*sum[j]=2*a*sum[i]*sum[j]+f[i]-a*sum[i]*sum[i]-b*sum[i]-c;

Then there are: y=kx+b;

Y=F[J]+ASUM[J]*SUM[J]-B*SUM[J];

X=SUM[J];

K=2*a*sum[i];

B=f[i]-a*sum[i]*sum[i]-b*sum[i]-c;


It is then apparent that each j is processed with an X and Y, which can be represented in the coordinate system.

And if there is an I, then can be processed out of "K", then you can calculate the value of B.

And I, by the value of B, we can calculate the value of f[i].


B is the Intercept, then the subject of B and f[i] is proportional. We can begin by drawing a line with a slope of K and a J-Abstract point J to help understand.

Obviously, this intercept needs to be as large as possible to ensure the answer.


Then we need to maintain a convex hull according to the point, the brain can be mended, the points inside the convex hull may not be better (this does not elaborate), and we are looking for a convex point p, so that we first put the line at infinity, and then pan down, touching the first point on the convex packet.

By drawing you can visually discover that a straight line with a slope of K passes the point P with the maximum intercept for the current "I".


That is, for the current "I", the "J" represented by this p is the optimal J that can be transferred from the motion-gauge equation f[i]=f[j]+f (sum[i]-sum[j]).

That is, any f[i that is transferred by K (K!=J) will be less than that transferred through J F[i].


In general, we can find this bump p, but sometimes the slope satisfies monotonicity, we can use a maintenance method similar to the monotone queue to maintain the convex hull, such as the case, once the queue head point for I processing out of the slope k inferior to the second point in the queue, the head out of the team, In this way, the team head can be taken as the best decision J of I after the team operation is completed.


Then when the decision is finished we get the f[i], and then we can deal with the "X" and "Y", then add it to the convex bag.

Well, the convex bag won't say much.


Paste code, this code is very clear ~ ~ ~ (Maintenance of the convex hull ....) )

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 1001000# Define x (x) (sum[x]) #define Y (x) (f[x]+a*sum[x]*sum[x]-b*sum[x]) #define K (x) (2*a*sum[x]) #define F (x) (A*X*X+B*X+C) Using namespace Std;int l,r,n,a,b,c;long long sum[n],f[n];struct point{long long x,y;int ID; Point (Long long _x,long long _y,int _id): X (_x), Y (_y), id (_id) {}point () {}}que[n],now;inline long long Xmul (Point I,point J , point K) {return (j.x-k.x) * (I.Y-K.Y)-(J.Y-K.Y) * (i.x-k.x);} int main () {//freopen ("test.in", "R", stdin), int i,j,k;scanf ("%d%d%d%d", &n,&a,&b,&c); for (i=1;i<= n;i++) {scanf ("%d", &sum[i]), sum[i]+=sum[i-1];k=k (i); while (L<r&&que[l].y-k*que[l].x<que[l+1]. y-k*que[l+1].x) l++;f[i]=f[que[l].id]+f ((sum[i]-sum[que[l].id)) Now=point (x (i), Y (i), i); while (l<r&& Xmul (Now,que[r],que[r-1]) >=0) R--;que[++r]=now;} Cout<<f[n]<<endl;return 0;}



"BZOJ1911" "Apio2010" Special Ops team, slope optimization dp topless (slope monotonic)

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.