土地并购 (Land Acquisition, USACO 2008 Mar)

题目描述

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

约翰准备扩大他的农场,眼前他正在考虑购买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 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.

斜率优化dp

#include<bits/stdc++.h>
#define ll long long
using namespace std;
struct node
{
	ll x,y;
    friend bool operator <(node a,node b)
    {
        if(a.x==b.x) return a.y<b.y;
        else return a.x<b.x;
    }
}d[50005],p[50005];
ll f[50005];
int l=1,r=1,que[50005];
double slope(int j,int k)
{
    return (double)(f[j]-f[k])/(p[k+1].y-p[j+1].y);
}
int main()
{
	int n;
    scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%lld%lld",&d[i].x,&d[i].y);
	sort(d+1,d+n+1);
	int cnt=0;
	for(int i=1;i<=n;i++)
	{
		while(cnt&&d[i].y>=p[cnt].y) cnt--;
		p[++cnt]=d[i];
	}
    que[1]=0;
	for(int i=1;i<=cnt;i++)
	{
        while( l<r && slope(que[l],que[l+1]) < p[i].x ) l++;
        int front=que[l];
        f[i]=f[front]+p[i].x*p[front+1].y;
        while( l<r && slope(que[r-1],que[r]) > slope(que[r],i) ) r--;
        que[++r]=i;
    }
	printf("%lld",f[cnt]);
}

  

时间: 2024-10-06 00:21:25

土地并购 (Land Acquisition, USACO 2008 Mar)的相关文章

bzoj1597【USACO 2008 Mar】土地购买

1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 3169  Solved: 1183 [Submit][Status][Discuss] Description 农夫John准备扩大他的农场,他正在考虑N (1 <= N <= 50,000) 块长方形的土地. 每块土地的长宽满足(1 <= 宽 <= 1,000,000; 1 <= 长 <= 1,000,000). 每块

[USACO 2008 MAR] 土地购买

[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1597 [算法] 首先将所有土地按长为第一关键字 , 宽为第二关键字排序 显然 , 当i > j , 且yi >= yj时 , 土地j没有用 , 不妨使用单调栈弹出所有没有用的土地 用fi表示前i块土地的最小经费 显然 , fi = min{ fj + aibj } 斜率优化即可 时间复杂度 : O(N) [代码] #include<bits/stdc++.h> usi

BZOJ 1597 Usaco 2008 Mar 土地购买 斜率优化DP

题目大意:给出一些木板,现在要购买这些木板.购买的规则是可以一些木板一起买,然后价格是最大的长度乘最大的宽度.求购买所有木板的最小费用. 思路:如果一个木板的长也比一个木板小,宽也比一个木板小,那么这个木板就可以被排除.把所有木板按照x的长度排序,然后去掉排除的木板,然后剩下的木板就是x值下降, y值上升的木板.这样的话我们买下连续的一段的费用就是x[j] * y[i],然后DP方程就很简单了:f[i] = f[j] - x[j + 1] * y[i]. 注意到数据范围,写一个斜率优化就水过了.

【BZOJ】【1597】【USACO 2008 Mar】土地购买

DP/斜率优化 Orz Hzwer…… 想到排序了,但没想到其实可以将序列转化为x递增且y递减的序列……因为x是递增的,若y[i]>y[i-1]那么第i-1个就足够小……以至于可以在搞定第 i 个的同时顺便带走…… 这次仔细写一下斜率优化的过程吧- 方程:$ f[i]=min\{ f[j]+x[i]*y[j+1] \} $ 若 $j>k$ 且 决策$j$更优,则有:\[ \begin{aligned} {f[j]+x[i]*y[j+1]} &<  {f[k]+x[i]*y[k+1

【USACO 2008 Mar Gold】 3.Pearl Pairing 贪心 pq

题意:有若干个颜色,每个颜色有若干头牛. 现在将牛进行配对,使得每对颜色都不一样,有SPJ. 题解:一旦某种颜色的牛数量占当前未配对牛总数最多,那么就要群起而攻之! 利用pq或者heap解决. 代码: #include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 101000 #define inf

BZOJ 1617 Usaco 2008 Mar. River Crossing渡河问题

[题解] 显然是个DP题. 设$f[i]$表示送$i$头牛过河所需的最短时间,预处理出$t[i]$表示一次性送i头牛过河所需时间,那么我们可以得到转移方程:$f[i]=min(f[i],f[i-j]+t[j]+t[0])$ (这里的$t[0]$指的是FJ独自过河的时间) 这样就可以做一个$n$方的DP了 #include<cstdio> #include<algorithm> #define rg register #define inf (1e9) #define N (1000

『土地征用 Land Acquisition 斜率优化DP』

斜率优化DP的综合运用,对斜率优化的新理解. 详细介绍见『玩具装箱TOY 斜率优化DP』 土地征用 Land Acquisition(USACO08MAR) Description 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

【笔记篇】斜率优化dp(五) USACO08MAR土地购(征)买(用)Land Acquisition

好好的题目连个名字都不统一.. 看到这种最大最小的就先排个序嘛= =以x为第一关键字, y为第二关键字排序. 然后有一些\(x_i<=x_{i+1},且y_i<=y_{i+1}\)的土地就完全可以在买\(i+1\)的时候顺便把\(i\)买了. 那么现在就剩下了x递增 y递减的一串. 可以证明一次一起买的应该是连续的一段, 因为中间的y一定比左端点的大, x一定比右端点的小, 那么一定可以在买左右端点的同时把中间的买走. 那就是常见套路了, 方程\(f[i]=f[j]+x[i]*y[\)\(j+

洛谷P2900 [USACO08MAR]土地征用Land Acquisition

题目:https://www.luogu.org/problemnew/show/P2900 题目描述 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 <=