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 length and width of the land of Block I
Output
* First line: The minimum feasible cost.
Sample Input
4 - 1 the the - 5 1 -
Input explanation:
There are 4 pieces of land.
Sample Output
500
HINT
FJ 3 groups to buy these lands: The first group: 100x1, the second group of 1x100, the third group of 20x5 and 15x15 plot. The prices for each group were 100,100,300, a total of 500.
Solution
The first slope optimization problem.
When we see the topic, we can find that for every large and wide land, the land that is smaller than its length and width in the group including it will not affect the result.
So let's start with the first keyword of the land, the width of the second keyword to be sorted, will be large land containing small rectangles deleted
So the simple dynamic programming equation is as follows
F[I]=MIN{F[J]+X[I]*Y[J+1]}
Slope optimization is now
F[j]+x[i]*y[j+1 in the original type] is V-type
For A<b,v (a) >v (b),
(f[b]-f[a])/(y[a+1]-y[b+1])<x[a]
Then, the decision as a point is drawn in the plane Cartesian coordinate system, the maintenance of the decision function convex
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intn=50001; typedefLong Longll;intN,q[n],tot;ll X[n],y[n],f[n];structland{ll x, y; BOOL operator< (ConstLand h)Const{ if(! (x^h.x))returny<H.y; returnx<h.x; }}a[n];inlineDoubleSlopeintAintb) { return(F[b]-f[a])/(y[a+1]-y[b+1]);}intMain () {scanf ("%d",&N); for(intI=1; i<=n;i++) scanf ("%lld%lld",&a[i].x,&a[i].y); Sort (a+1, A +1+N); for(intI=1; i<=n;i++){ while(Tot&&a[i].y>=y[tot]) tot--; x[++tot]=a[i].x;y[tot]=a[i].y; } intL=0, r=0; for(intI=1; i<=tot;i++){ while(L<r&&slope (q[l],q[l+1]) <x[i]) l++; intt=Q[l]; F[i]=f[t]+x[i]*y[t+1]; while(L<r&&slope (Q[r],i) <slope (q[r-1],Q[R]) r--; q[++r]=i; } printf ("%lld\n", F[tot]); return 0;}
[bzoj1597] [usaco2008 Mar] Land purchase