[USACO08MAR]土地征用Land Acquisition

本文深入解析USACO竞赛中“土地征用”问题,探讨如何通过巧妙分组,利用宽度和长度的优势来最小化总购买成本。文章提供了一种基于排序和单调队列的解决方案,通过DP算法实现最优购买策略。

[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 3x5 plot and a 5x3 plot in a group, he will pay 5x5=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

 

输入输出格式

输入格式:

 

* 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 100x1 plot and costs 100. The next group contains a 1x100 plot and costs 100. The last group contains both the 20x5 plot and the 15x15 plot and costs 300. The total cost is 500, which is minimal.

 

翻译:

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

思路:

首先,我们按照x排序,用单调队列维护出来没有任何一块土地的a[i].x<=a[j].x && a[i].y<=a[j].y,然后如果x按升序,那么y一定降序dp[i]=max(dp[j]+a[j+1].y*a[i].x); 斜率优化即可

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define rep(i,a,b) for(long long i=a;i<=b;i++)
#define MAXN 50005
using namespace std;
long long n,m,q[MAXN],l,r,dp[MAXN];
deque<long long> Q;
struct zlk{
	long long x,y;
}a[MAXN],b[MAXN];
bool cmpp(zlk t,zlk r){
	if(t.x!=r.x) return t.x<r.x;
	return t.y<r.y;
}
int main(){
	scanf("%lld",&n);
	rep(i,1,n) scanf("%lld%lld",&a[i].x,&a[i].y);
	sort(a+1,a+n+1,cmpp);
	Q.push_front(1);
	rep(i,2,n){
		while(!Q.empty() && a[Q.back()].y<=a[i].y) Q.pop_back();
		Q.push_back(i);
	}
	while(!Q.empty()) b[++m].x=a[Q.front()].x,b[m].y=a[Q.front()].y,Q.pop_front();
	l=r=1;
	rep(i,1,m){
		while(l<r && (dp[q[l+1]]-dp[q[l]])<=b[i].x*(b[q[l]+1].y-b[q[l+1]+1].y)) ++l;
		dp[i]=dp[q[l]]+b[i].x*b[q[l]+1].y;
		while(l<r && (dp[i]-dp[q[r]])*(b[q[r-1]+1].y-b[q[r]+1].y)<=(dp[q[r]]-dp[q[r-1]])*(b[q[r]+1].y-b[i+1].y)) --r;
		q[++r]=i;
	}
	printf("%lld",dp[m]);
}

 

  

 

转载于:https://www.cnblogs.com/handsome-zlk/p/10234847.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值