"BZOJ-1597" land purchase DP + slope optimization

Source: Internet
Author: User

1597: [Usaco2008 Mar] Land Purchase time limit:10 Sec Memory limit:162 MB
submit:2931 solved:1091
[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

Solution

DP Nothing to say, as far as the data range, is definitely optimized to $o (N/NLOGN) $, then consider the slope optimization

The main idea is to divide multiple groups, the maximum per group * Each group is the widest value per group, the minimum value required

It is easy to find that if a piece of land, his length and width are less than equal to the longest and longest width of his group, then this piece of land is not necessary to exist

Then consider sorting the original data and using a monotonic stack to maintain the length and width to achieve the goal

So easy to draw transfer $DP [I]=min (Dp[j]+y[j+1]*x[i]) $

Then the slope optimization gets $ (dp[j-1]-dp[k-1])/(Y[j]-y[k]) >-x[i]$

Code
#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>using namespacestd;intRead () {intx=0;CharCh=GetChar ();  while(ch<'0'|| Ch>'9') ch=GetChar ();  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx;}#defineMAXN 50010intN,top,que[maxn],l,r;Long LongSTACKX[MAXN],STACKY[MAXN],DP[MAXN];structfieldnode{intb; BOOL operator< (ConstFieldnode & A)Const        {            if(A==A.A)returnb<a.b; returna<A.A; }}FIE[MAXN];DoubleSlopeintIintj) {return(Dp[j]-dp[i])/(stacky[i+1]-stacky[j+1]);}intMain () {n=read ();  for(intI=1; i<=n; i++) Fie[i].a=read (), fie[i].b=read (); Sort (fie+1, fie+n+1);  for(intI=1; i<=n; i++)        {             while(Top && fie[i].b>=stacky[top]) top--; Top++; STACKX[TOP]=FIE[I].A; stacky[top]=fie[i].b; }     for(intTmp,i=1; i<=top; i++)        {             while(L<r && Slope (que[l],que[l+1]) <stackx[i]) l++; TMP=Que[l]; Dp[i]=dp[tmp]+stackx[i]*stacky[tmp+1];  while(L<r && Slope (que[r],i) <slope (que[r-1],QUE[R]) r--; que[++r]=i; } printf ("%lld\n", Dp[top]); return 0;} 

A cow problem made it is also drunk ...

"BZOJ-1597" land purchase DP + slope optimization

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.