Ⅰ of decision Monotonicity: Quadrilateral Inequalities:
http://blog.csdn.net/jaihk662/article/details/78174717
Decision Monotonicity:
For all x>y in dp[i] = min (Dp[i], dp[j]+w[j, I]), must satisfy K[x]>=k[y], where k[x] represents the optimal decision point for X-Points
That is, each decision point can be decision-making interval must be a continuous paragraph, and with the decision point of the right shift, the interval is also moving right
This allows you to use a stack or a binary O (NLOGN) to resolve the original O (n²) DP
The slope optimization can handle some special monotone DP, and can do the complexity of O (n)
Or This example: 1010: [HNOI2008] Toy boxing toy
The DP equation for this question is: dp[i] = min (dp[j]+ (sum[i]-sum[k]+i-j-1-l) ²) (where sum[] is prefixed and monotonically incremented)
Set A[i] = sum[i]+i-1-l;b[i] = Sum[i]+i
with dp[i] = min (dp[j]+ (a[i]-b[j]) ²) = A[i]²+min ( -2a[i]b[j]+b[j]²+dp[j])
Re-set A[i] = -2a[i],x (j) = B[j],y (j) = B[j]²+dp[j]
Then the equation becomes the classic form: f (i) = Min (A[i]*x (j) +y (j)), which is G = ax+y (y =-ax+g)
And our goal is to find a group (x, y) that makes G the smallest
This is equivalent to a number of points in space (x ', Y '), and there is a slope has been determined by a straight line, so that the straight through a certain point, so that the line and the y-axis of the minimum intercept, and because A[i] monotonically increasing, a =-a[i], the slope is a, so this slope must be monotonically increasing, Likewise, X and Y are monotonically increasing.
Suppose n=6, the following diagram is modeled:
The points in the graph are numbered from right to 1,2,3,4,5,6, corresponding to 6 decision points (don't forget that the No. 0 point is the origin of the coordinates) .
The two green lines in the figure are the 1th, 3 straight lines with slopes of 2a[1] and 2a[3], and the other lines are omitted without drawing
0 o'clock in the team.
When the first decision point is traversed, the queue has only 1 elements and is directly into the team
When traversing the second decision point, the queue has 2 elements (0 and 1) that determine the slope of the line that passes through the No. 0 and 1th points (for convenience, the line that passes X and Y points is omitted as straight XY) is greater than the current slope 2a[2]
If it is less than, the No. 0 point is 1th points after the explosion, from the next straight through 1th points will definitely be better than after No. 0 points, so pop-up No. 0 points
Otherwise, we assume that is greater than, that is, the current or No. 0 points or the best decision, a face to see the following image (the picture is greater than the case)
Is the case: where Pink represents a straight line 01, the Green line represents the slope 2a[2] of the line, we may wish to translate the green line downward, until the green Line covering 1th decision-making points, then you can find the obvious or pink intercept smaller, which means that the pink G is smaller, or No. 0 points more excellent, so do not play
Then compare the slope of the line 01 and the slope of the line 12, to see if the former is less than the latter, if not satisfied, the description of 1th points to be ejected, because the next point to choose No. 0 points, or choose 2nd points, absolutely no 1th points, 1th points are, complete explosion (this is actually in the maintenance under the convex package.) )
The figure 1th above is obviously not to be bounced off, so no point is kicked out after traversing the second point, and the current optimal decision point or the number No. 0 point
2nd points into the team, at this time from the end of the team to the first element of 0,1,2
It's time to traverse the third point, and then look back at the first figure, and you'll see clearly that point No. 0 should be bounced off (the line 01 slope is significantly less than 2a[3])
Then the No. 1th Point obviously will not be bounced off, the end of the team, and then look at the team head, the same maintenance under the convex bag, the same look at the picture can be a single eye to determine the point of 2nd to be ejected.
Enter the team at number 3rd, from the end of the team to the first element of the team is 1, 3
......
This makes a trip down, because each point will only enter the stack 1 times, so the successful in O (n) time to find out all the best decision.
Here is the road template problem
1597: [Usaco2008 Mar] Land purchase Time Limit:10 Sec Memory limit:162 MB
submit:5286 solved:1961
[Submit] [Status] [Discuss] Description
Farmer John prepares to expand his farm, and he is considering N (1 <= n <= 50,000) blocks of rectangular land. The length and width of each land is satisfied (1 <= wide <= 1,000,000; 1 <= long <= 1,000,000). The price of each piece of land is its area, but FJ can buy much faster land at the same time. The price of these lands is their largest length multiplied by their maximum width, but the length of the land cannot be exchanged. If FJ buys a 3x5 land and a 5x3, he needs to pay 5x5=25. FJ wanted to buy all the land, but he found that grouping to buy the land would save money. He needs you to help him find the minimum funds. Input
* Line 1th: one number: N
* 2nd. N+1 Line: Line i+1 contains two numbers, respectively, the long and wide Output of block I land
* First line: The minimum feasible cost. Sample Input 4 1 5 1 sample Output 500
First an obvious greed, all the land by x[] from small to large sort, x[] the same as y[] from small to large sort
This way down, all the land in each group must be contiguous.
With DP equation: dp[now] = min (Dp[now], Dp[j]+x[now]*max (Y[i], i∈ (j+1, now))
Theoretically can slope optimization when the segment tree maintains the maximum value, but this is quite troublesome
Consider again greedy: if a piece of land after the x[] value is less than the next block (in fact, this is definitely ordered) and y[] value is less than the next piece, then this piece can be ignored, because then you only use more than the selection of the piece behind it actually OK, this piece must not contribute, It must not be the optimal solution to contribute.
When this is done, the entire sequence must satisfy the x increment while y decrements, and the DP equation becomes
Dp[now] = min (Dp[now], dp[j]+x[now]*y[j+1])
Turn it into a standard form: y =-ax+g has a = X[now];x = Y[j+1];y = Dp[j]
Because the slope k and X are monotonically decreasing, y is monotonically increasing, so to maintain the convex hull, there are equations
-x[now]< (Dp[j]-dp[k])/(y[j+1]-y[k+1]), it means that K points are better than J points
#include <stdio.h>
#include <algorithm>
using namespace std;
#define LL Long long
typedef struct RES
{
LL x, y;
BOOL operator < (const Res &b) Const
{
if (x<b.x | | x==b.x && Y<B.Y)
return 1;
return 0;
}
} Res;
Res s[50005];
LL st[50005], dp[50005];
Double Calc (ll J, ll K)
{
return 1.0* (Dp[j]-dp[k])/(S[J+1].Y-S[K+1].Y);
}
int main (void)
{
LL n, I, CNT, L, R;
scanf ("%lld", &n);
for (i=1;i<=n;i++)
scanf ("%lld%lld", &s[i].x, &s[i].y);
Sort (s+1, s+n+1);
CNT = 0;
for (i=1;i<=n;i++)
{
while (cnt>=1 && s[i].y>=s[cnt].y)
cnt--;
S[++CNT] = S[i];
}
n = cnt;
L = R = 1;
for (i=1;i<=n;i++)
{while
(r>l && Calc (St[l], st[l+1]) >-s[i].x)
l++;
Dp[i] = dp[st[l]]+s[i].x*s[st[l]+1].y;
while (R>l && Calc (st[r], i) >calc (St[r-1], st[r]))
r--;
St[++r] = i;
}
printf ("%lld\n", Dp[n]);
return 0;
}