1597: [Usaco2008 Mar] Land purchase time limit: ten Sec Memory Limit: 162 MB
Submit: 3169 Solved: 1183
[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 length and width of the land of Block I
Output
* First line: The minimum feasible cost.
Sample Input4
100 1
15 15
20 5
1 100
Input explanation:
There are 4 pieces of land.
Sample Output500
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.
Source
Gold
Slope Optimization DP
by x from small to large sort, if two points I J satisfies Xi≤xj and yi≤yj, that I there is no need to exist, can be deleted.
So the last resulting sequence is an x increment y descending sequence.
F[i] represents the minimum value of the first I element, then f[i]=min{f[j-1]+y[j]x[i]}.
Then the slope optimization can be done casually.
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath > #include <algorithm> #define F (I,j,n) for (int. i=j;i<=n;i++) #define D (i,j,n) for (int i=j;i>=n;i--) # Define ll long long#define MAXN 50005#define eps 1e-8using namespace std;int n,tot,l,r,q[maxn];struct data{ll x, y;} A[maxn];ll f[maxn];inline int Read () {int X=0,f=1;char ch=getchar (); while (ch< ' 0 ' | | Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} inline BOOL CMP (data A,data b) {return a.x!=b.x?a.x<b.x:a.y<b.y;} inline double getk (int x,int y) {return (double) (f[x-1]-f[y-1])/(double) (A[X].Y-A[Y].Y);} int main () {n=read (); F (I,1,n) A[i].x=read (), A[i].y=read (); sort (a+1,a+n+1,cmp); F (I,1,n) {while (TOT&&A[TOT].Y<=A[I].Y) tot--;a[++tot]=a[i];} n=tot;l=1;r=0; F (I,1,n) {while (L<R&&GETK (Q[r-1],q[r]) <GETK (q[r],i)) R--;q[++r]=i;while (L<R&&GETK (q[l), Q[L+1]) > (double)-a[i]. x) l++;f[i]=f[q[l]-1]+a[q[l]].y*a[i].x;} printf ("%lld\n", F[n]); return 0;}
bzoj1597 "Usaco Mar" land purchase