[Luogu P2900][USACO08MAR]土地征用Land Acquisition

[Luogu P2900][USACO08MAR]土地征用Land Acquisition

题目描述

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

4 
100 1 
15 15 
20 5 
1 100 

 

输出样例#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.

 

思路

斜率优化DP

 

代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

struct __{
	int x, y;
}_[50010];
int n, maxy, q[50010], head, tail;
long long f[50010], x[50010], y[50010];

bool cmp(const __ &A, const __ &B){return A.x==B.x ? A.y>B.y : A.x>B.x;}

int main(){
	memset(f, 0x3f3f3f3f, sizeof(f));
	f[0]=0;
	scanf("%d", &n);
	for(int i=1; i<=n; i++)	scanf("%d%d", &_[i].x, &_[i].y);
	sort(_+1, _+n+1, cmp);
	for(int i=1; i<=n; i++)
		if(maxy<_[i].y)	x[++x[0]]=_[i].x, y[++y[0]]=_[i].y, maxy=_[i].y;
	q[1]=0;
	head=1, tail=1;
	for(int i=1; i<=x[0]; i++){
		while(y[i]*(x[q[head]+1]-x[q[head+1]+1])>=f[q[head+1]]-f[q[head]] && head<tail)	head++;
		f[i]=x[q[head]+1]*y[i]+f[q[head]];
		while((f[q[tail]]-f[q[tail-1]])*(x[q[tail]+1]-x[i+1])>(f[i]-f[q[tail]])*(x[q[tail-1]+1]-x[q[tail]+1]) && head<tail)	tail--;
		q[++tail]=i;
	}
	cout<<f[x[0]]<<endl;
	return 0;
}

 

 

发表评论

邮箱地址不会被公开。 必填项已用*标注