Slope optimization Topic 1 -- bzoj 1597 [usaco2008 Mar] land purchase question

Source: Internet
Author: User

Reprinted Please note: http://blog.csdn.net/jiangshibiao/article/details/24387147

[Original question]

1597: [usaco 162 Mar] land purchase time limit: 10 sec memory limit: MB
Submit: 1396 solved: 480
[Submit] [Status] Description

Farmer John is planning to expand his farm and he is considering N (1 <= n <= 50,000) rectangular land. the length and width of each piece of land are equal to (1 <= width <= 1,000,000; 1 <= length <= 1,000,000 ). the price of each piece of land is its area, but FJ can buy at the same time. the price of these lands is their maximum length multiplied by their maximum width, but the length and width of the lands cannot be exchanged. suppose FJ buys a 3x5 location and a 5x3 location, then it needs to pay 5x5 = 25. FJ wants to buy all the land, but he finds that grouping the land can save money. he needs you to help him find the minimum fund.

Input

* Row 1st: number of rows: N

* Row 2nd. n + 1: Row I + 1 contains two numbers, the length and width of the I-th land.

Output

* Row 1: minimum viable costs.

Sample input4
100 1
15 15
20 5
1 100

Input explanation:

4 pieces of land are jointly owned.

Sample output500

Hint

FJ buys the land in three groups: the first group: 100x1, the second group 1x100, and the third group 20x5 and 15x15 plot. the price for each group is 100,100,300, 500 in total.

Source

Gold


[Analysis] Amazing! I read jokerpark's blog and benefited a lot. However, I did not understand it in some places, so I also pushed the formula for half a day.

Set the length and width of block I to AI and Bi.

① Consider invalid edges first. Set AI> = AJ and Bi> = BJ, then J must be invalid. How can this problem be solved? First, we sort AI in ascending order, and then perform operations similar to monotonous queues. Enumeration I. If Bi> = BJ at the end of the queue, the tail pointer is reduced by 1. Since a has been sorted in ascending order, AI> = AJ is positive.

② After removing the edge, we also found an interesting property. In the queue, because a must be sorted in ascending order, B must be in descending order. (Own YY)

③ The DP equation is as follows: F [I] = max (F [I], F [J] + A [J + 1] * B [I]). There is no doubt that this is a time-out algorithm. Now we have to optimize the push slope !! When it is set in the current state f [I], the transfer from F [J] is better than F [K. Then f [J] + A [J + 1] * B [I] <F [k] + A [k + 1] * B [I]. Simplified: B [I] <(F [k]-f [J])/(A [J + 1]-A [k + 1]). Note: Assuming that J and K meet the above requirements, K can ignore it because J must be better than K. Based on this, we maintain a monotonous queue.

[Code]

#include<cstdio>#include<algorithm>#define N 50005using namespace std;struct arr{long long x,y;}a[N],b[N];long long f[N],q[N],n,i,h,t,m;bool cmp(arr a,arr b){return a.x<b.x||a.x==b.x&&a.y>b.y;}int main(){  scanf("%lld",&n);  for (i=1;i<=n;i++)    scanf("%lld%lld",&b[i].x,&b[i].y);  sort(b+1,b+n+1,cmp);  m=1;a[1].x=b[1].x;a[1].y=b[1].y;  for (i=2;i<=n;i++)  {    while (m&&b[i].y>a[m].y) m--;    a[++m].x=b[i].x;a[m].y=b[i].y;  }  h=1;t=1;q[1]=0;f[0]=0;  for (i=1;i<=m;i++)  {    while ((h<t)&&(a[i].x*(a[q[h]+1].y-a[q[h+1]+1].y)>f[q[h+1]]-f[q[h]])) h++;    f[i]=f[q[h]]+a[q[h]+1].y*a[i].x;    while ((h<t)&&(f[i]-f[q[t-1]])*(a[q[t]+1].y-a[i+1].y)>=(f[i]-f[q[t]])*(a[q[t-1]+1].y-a[i+1].y)) t--;    q[++t]=i;  }  printf("%lld",f[m]);  return 0;}

Slope optimization Topic 1 -- bzoj 1597 [usaco2008 Mar] land purchase question

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.