Land purchase (Bzoj 1597)

Source: Internet
Author: User

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 Output -
/*we can know that if xi<=xj&&yi<=yj, then x is useless.  So we sort by x to get rid of the useless points so that the transfer equation becomes f[i]=f[j-1]+x[j]*y[i].  Consider two transfer f[j],f[k], and k<j<i;  If transfer to f[i] from f[j] is better than from f[k], then f[j]+y[i]*x[j+1]<f[k]+y[i]*x[k+1]; The moving term is F (j), F (k+1)/x (k), × (j+1) <y (i) is then optimized with slope. */#include<cstdio>#include<iostream>#include<algorithm>#defineN 50010#defineLon Long Longusing namespaceStd;lon F[n];intN,m,q[n];structnode{intx, y;}; Node A[n],b[n];BOOLcmpConstNODE&AMP;S1,Constnode&S2) {    returns1.x>s2.x| | (s1.x==s2.x&&s1.y>s2.y);}DoublexvintJintk) {    return(F[j]-f[k])/(a[k+1].x-a[j+1].x);}intMain () {scanf ("%d",&N);  for(intI=1; i<=n;i++) scanf ("%d%d",&b[i].x,&b[i].y); Sort (b+1, b+n+1, CMP); M=1; a[1]=b[1];  for(intI=2; i<=n;i++){        if(B[I].Y&GT;A[M].Y) a[++m]=B[i]; }    intHead=1, tail=1; q[1]=0;  for(intI=1; i<=m;i++){        if(HEAD&LT;TAIL&AMP;&AMP;XV (q[head],q[head+1]) <a[i].y) head++; F[i]=f[q[head]]+ (Lon) a[q[head]+1].x*(Lon) A[I].Y;  while(HEAD&LT;TAIL&AMP;&AMP;XV (q[tail-1],q[tail]) &GT;XV (q[tail],i)) tail--; q[++tail]=i; } cout<<F[m]; return 0;}

Land purchase (Bzoj 1597)

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.