[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 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.

解题分析

我们用 wid(x) w i d ( x ) len(x) l e n ( x ) 分别表示土地的宽和长很明显, 如果存在一块土地 x x 和另一块土地y,使得 wid(x)wid(y) w i d ( x ) ≥ w i d ( y ) len(x)len(y) l e n ( x ) ≥ l e n ( y ) 那么显然我们在买 x x 时可以一并将y 买入。

所以我们可以将所有土地按 len l e n 为第一关键字, wid w i d 为第二关键字由大到小排序, 选取 wid w i d 更大的即可去掉干扰的土地。 同时这样处理后,土地的 len l e n 从大到小排列的同时 wid w i d 从小到大递增。我们可以得到转移方程:

dp[i]=min(dp[j]+len[j+1]×wid[i]) d p [ i ] = m i n ( d p [ j ] + l e n [ j + 1 ] × w i d [ i ] )

发现这个算法是 O(n2) O ( n 2 ) 的, 显然跑不过50000的数据量。
我们考虑两个转移点 j j k满足 j<k j < k 且从 k k 转移花费更小, 即:

dp[j]+len[j+1]×wid[i]<dp[k]+len[k+1]×wid[i]

移项后可得

wid[i]>dp[j]dp[k]len[k+1]len[j+1] w i d [ i ] > d p [ j ] − d p [ k ] l e n [ k + 1 ] − l e n [ j + 1 ]

发现左边是个递增的常数, 而右边与 i i 无关,并且是一个斜率的形式,所以我们可以用斜率优化和单调队列来维护转移点。每次我们处理队首时, 持续将满足要求的(即队首的下一个点最优)的队首弹出队列, 直至到达最优。

处理队尾的时候, 如果发现队尾和其上一个转移点的斜率比加入点和队尾的斜率还大的时候, 如图:

旁边的ShadyPi大佬说为了维护单调队列的单调性才将C踢出队列, 但博主认为BC的斜率过大, 导致难以转移, 而如果能够转移至C wid[i]肯定已大到可以转移至E点获得更优解, 所以C点显然是废的, 不如直接转移至E点。 所以出现这种情况时直接将C踢出队列。

那么现在思路应该很清晰了, 上代码:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <algorithm>
#define R register
#define W while
#define IN inline
#define gc getchar()
#define MX 50005
#define ll long long
#define db double
using namespace std;
ll ans, dp[MX];
int num, tot, que[MX], head, tail;
struct Land
{
    ll len, wid;
}land[MX], deal[MX];
IN bool operator < (const Land &x, const Land &y)
{return x.len == y.len ? x.wid > y.wid : x.len > y.len;}
template <class T>
IN void in(T &x)
{
    x = 0; R char c = gc;
    W (!isdigit(c)) c = gc;
    W (isdigit(c))
    {x = (x << 1) + (x << 3) + c - 48, c = gc;}
}
IN db slope(const int &a, const int &b)
{
    return (db)(dp[b] - dp[a]) / (db)(deal[a + 1].len - deal[b + 1].len);
}
int main(void)
{
    in(num);
    for (R int i = 1; i <= num; ++i)
    in(land[i].len), in(land[i].wid);
    sort (land + 1, land + 1 + num);
    for (R int i = 1; i <= num; ++i)
    {if(land[i].wid > deal[tot].wid) deal[++tot] = land[i];}
    for (R int i = 1; i <= tot; ++i)
    {
        W (head < tail && slope(que[head], que[head + 1]) <= deal[i].wid) head++;
        dp[i] = dp[que[head]] + deal[que[head] + 1].len * deal[i].wid;
        W (head < tail && slope(que[tail - 1], que[tail]) > slope(que[tail], i)) tail--;    
        que[++tail] = i;
    }
    printf("%lld", dp[tot]);
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值