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.
DP Slope optimization
For land (x1,y1) and (X2,y2), if x1≥x2 and Y1≥y2, it can be ignored (x2, y2)
Sort the land in descending order of x ascending y, ignoring land that is not to be considered
F[i]=max{f[j]+x[i]*y[j+1]} (0<j<i)
If K is better than J f[j]-f[k]>x[i]* (Y[k+1]-y[j+1])
#include <cstdio>#include<algorithm>structpos{Long Longx, y;} vs[50001],ps[50001];intpp=1;BOOL operator<(pos A,pos b) {if(a.x==b.x)returna.y<b.y; returna.x<b.x;}Long Longf[50001];intq[50001]={0};intqs=0, qe=0;intN;inlineLong LongAintXinty) { returnf[x]-f[y];} InlineLong LongBintXinty) { returnps[y+1].y-ps[x+1].y;}intMain () {scanf ("%d",&N); for(intI=0; i<n;i++) scanf ("%d%d",&vs[i].x,&vs[i].y); Std::sort (Vs,vs+N); for(intI=0; i<n;i++){ while(pp>1&&ps[pp-1].Y<=VS[I].Y)--pp; PS[PP++]=Vs[i]; } for(intI=1; i<pp;i++){ while(Qs<qe&&a (q[qs],q[qs+1]) >ps[i].x*b (q[qs],q[qs+1]))++QS; intw=Q[qs]; F[i]=f[w]+ps[i].x*ps[w+1].y; while(Qs<qe&&a (q[qe-1],Q[QE]) *b (q[qe],i) >=a (q[qe],i) *b (q[qe-1],Q[QE])--QE; q[++qe]=i; } printf ("%lld", f[pp-1]); return 0;}
bzoj1597 Land Purchase