P2900 [USACO08MAR]土地征用Land Acquisition[斜率优化dp]

题目描述

Farmer John is considering buying more land for the farm and has his eye on N ($1 <= N <= 50,000$ additional rectangular plots, each with integer dimensions ($1 <= width_i <= 1,000,000$; $1 <= length_i <= 1,000,000$).

If FJ wants to buy a single piece of land, the cost is ¥1/square unit, but savings are available for large purchases. He can buy any number of plots of land for a price in dollars that is the width of the widest plot times the length of the longest plot. Of course, land plots cannot be rotated, i.e., if Farmer John buys a $3×5$ plot and a $5×3$ plot in a group, he will pay $5×5=25$

FJ wants to grow his farm as much as possible and desires all the plots of land. Being both clever and frugal, it dawns on him that he can purchase the land in successive groups, cleverly minimizing the total cost by grouping various plots that have advantageous width or length values.

Given the number of plots for sale and the dimensions of each, determine the minimum amount for which Farmer John can purchase all

约翰准备扩大他的农场,眼前他正在考虑购买N块长方形的土地。如果约翰单买一块土 地,价格就是土地的面积。但他可以选择并购一组土地,并购的价格为这些土地中最大的长 乘以最大的宽。比如约翰并购一块 $3 × 5​$ 和一块 $5 × 3 ​$的土地,他只需要支付 $5 × 5 = 25​$ 元, 比单买合算。 约翰希望买下所有的土地。他发现,将这些土地分成不同的小组来并购可以节省经费。 给定每份土地的尺寸,请你帮助他计算购买所有土地所需的最小费用。

输入输出格式

输入格式:

  • Line 1: A single integer: $N$
  • Lines 2.. $N+1$: Line $i+1$ describes plot i with two space-separated integers: $width_i$ and $length_i$

输出格式:

  • Line 1: The minimum amount necessary to buy all the plots.

输入输出样例

输入样例#1:

1
2
3
4
5
4 
100 1
15 15
20 5
1 100

输出样例#1:

1
500

说明

There are four plots for sale with dimensions as shown.

The first group contains a 100×1 plot and costs 100. The next group contains a 1×100 plot and costs 100. The last group contains both the 20×5 plot and the 15×15 plot and costs 300. The total cost is 500, which is minimal.

题解

$O(n^2)$ 的方法很显然,按照 $x$ 从大到小排序,然后对于 $i,j$ , $X_i>X_j$ 且 $Y_i>Y_j$ ,j可以直接去掉(因为$i$的可以直接取掉。

转移方程:$fi=min(f{j-1}+x_j*y_i) (1\le j\le i)$

然后就可以加斜率优化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<bits/stdc++.h>
using namespace std;
int qmax(int &x,int y) {if (x<y) x=y;}
int qmin(int &x,int y) {if (x>y) x=y;}
inline int read()
{
char s;int k=0,base=1;
while((s=getchar())!='-'&&s!=EOF&&!(isdigit(s)));
if(s==EOF)exit(0);if(s=='-')base=-1,s=getchar();
while(isdigit(s)){k=k*10+(s^'0');s=getchar();}
return k*base;
}
void write(int x)
{
if(x<0){putchar('-');write(-x);}
else{if(x/10)write(x/10);putchar(x%10+'0');}
}
long long f[100000],x[60000],y[60000];
int n,tot;
int q[100000],l,r;
struct node {int x,y;} a[100000];
bool cmp(node aa,node bb) {return aa.x==bb.x?aa.y>bb.y:aa.x>bb.x;}
bool check1(int i,int j,int k)
{
if (y[i]*(x[k+1]-x[j+1])<=f[j]-f[k]) return true; return false;
}
bool check(int i,int j,int k)
{
if ((f[k]-f[j])*(x[i+1]-x[j+1])>=(f[j]-f[i])*(x[j+1]-x[k+1])) return true; return false;
}
int main()
{
n=read();
for (int i=1;i<=n;i++) a[i].x=read(),a[i].y=read();
sort(a+1,a+n,cmp);
tot=1;x[1]=a[1].x;y[1]=a[1].y;
for (int i=2;i<=n;i++)
{
if (a[i].y>y[tot]) tot++,x[tot]=a[i].x,y[tot]=a[i].y;
}
int l=1;r=1; n=tot;
for (int i=1;i<=n;i++)
{
while (l+1<=r&&check1(i,q[l],q[l+1])) l++;
f[i]=f[q[l]]+x[q[l]+1]*y[i];
while (l+1<=r&&check(i,q[r],q[r-1])) r--;
r++;q[r]=i;
}
printf("%lld\n",f[n]);
return 0;
}