洛谷-2900 [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: w i d t h i width_i widthiand l e n g t h i length_i lengthi

输出格式
Line 1: The minimum amount necessary to buy all the plots.

输入输出样例
输入 #1
4
100 1
15 15
20 5
1 100

输出 #1
500

斜率优化,只不过我们需要先排序一下土地,从小大到, x [ i ] > x [ j ] x[i]>x[j] x[i]>x[j] y [ i ] > y [ j ] y[i]>y[j] y[i]>y[j],j不对答案做贡献,直接舍弃,那么剩余的就是 x [ i ] > x [ j ] x[i]>x[j] x[i]>x[j] y [ i ] < y [ j ] y[i]<y[j] y[i]<y[j],那么 d p [ i ] = d p [ j ] + x [ i − 1 ] ∗ y [ j + 1 ] dp[i]=dp[j]+x[i-1]*y[j+1] dp[i]=dp[j]+x[i1]y[j+1],
( d p [ j ] − d p [ k ] ) / ( y [ k + 1 ] − y [ j + 1 ] ) < x [ i ] (dp[j]-dp[k])/(y[k+1]-y[j+1])<x[i] (dp[j]dp[k])/(y[k+1]y[j+1])<x[i]老套路了

#include <cstdio>
#include <algorithm>
const int N=5e4+5;
int n,q[N];
long long f[N];
struct node{
    int x,y;
    bool operator < (const node &b) const {
        return x==b.x?y>b.y:x>b.x;
    }
}a[N];

void init(){
    std::sort(a+1,a+n+1);
    int m=0;
    for(int i=1;i<=n;++i) if(a[i].y>a[m].y) a[++m]=a[i];
    n=m;
}
double cal(int i,int j){
    return 1.0*(f[i]-f[j])/(a[j+1].x-a[i+1].x);
}
int main() {
    scanf("%d",&n);
    for(int i=1;i<=n;++i) scanf("%d%d",&a[i].x,&a[i].y);
    init();
    for(int i=1,l=1,r=1;i<=n;++i) {
        while(l<r&&cal(q[l],q[l+1])<=a[i].y) ++l;
        f[i]=f[q[l]]+1LL*a[q[l]+1].x*a[i].y;
        while(l<r&&cal(q[r-1],q[r])>=cal(q[r],i)) --r;
        q[++r]=i;
    }
    printf("%lld\n",f[n]);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值