土地征用(斜率优化)

题目描述

Farmer John is considering buying more land for the farm and has his eye on N(1N50,000) N ( 1 ≤ N ≤ 50 , 000 ) additional rectangular plots, each with integer dimensions ( 1widthi1,000,000 1 ≤ w i d t h i ≤ 1 , 000 , 000 ; 1<=lengthi1,000,000 1 <= l e n g t h 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 3 x 5 plot and a 5x3 5 x 3 plot in a group, he will pay 5x5=25 5 x 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 3 × 5 和一块 5×3 5 × 3 的土地,他只需要支付 5×5=25 5 × 5 = 25 元, 比单买合算。 约翰希望买下所有的土地。他发现,将这些土地分成不同的小组来并购可以节省经费。 给定每份土地的尺寸,请你帮助他计算购买所有土地所需的最小费用。

输入输出格式

输入格式:

  • Line 1: A A single integer: N

  • Lines 2.. N+1 N + 1 : Line L i n e i+1 i + 1 describes plot i with two space-separated integers: widthi w i d t h i and lengthi l e n g t h 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 100 x 1 plot and costs 100 100 . The next group contains a 1x100 1 x 100 plot and costs 100. The last group contains both the 20x5 20 x 5 plot and the 15x15 15 x 15 plot and costs 300 300 . The total cost is 500, which is minimal.

题解

这是一道斜率优化的版题,这里写一下做这种题的一般姿势。
首先推出dp转移方程,一般是 fi=max f i = m a x { fj+aibj f j + a i ∗ b j }, jS j ∈ S
然后假设 k>j k > j k,jS k , j ∈ S ,列出 fj+aibj<fk+aibk f j + a i ∗ b j < f k + a i ∗ b k
接着可以得出 fjfkbkbj<ai f j − f k b k − b j < a i ,就可以用单调队列维护了。

Code

#include<bits/stdc++.h>
using namespace std;

int n,tot=0,que[50010],l=1,r=0;
long long f[50010];
struct matrix{
    long long a,b;
    bool operator < (const matrix &t)const{
        if(b!=t.b)return b<t.b;
        return a<t.a;
    }
}mm[50010],m[50010];

double cal(int j,int k){        //j<k
    if(m[j+1].a==m[k+1].a)return f[k]>f[j]?-1926081777:1926081777;
    return (double)(f[k]-f[j])/(double)(m[j+1].a-m[k+1].a);
}

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%lld%lld",&mm[i].a,&mm[i].b);
    sort(mm+1,mm+n+1);
    for(int i=1;i<=n;i++){
        while(tot&&m[tot].a<=mm[i].a)tot--;
        m[++tot]=mm[i];
    }
    f[0]=0;que[++r]=0;;
    for(int i=1;i<=tot;i++){
        while(l<r&&cal(que[l],que[l+1])<=(double)m[i].b)l++;
        f[i]=f[que[l]]+m[i].b*m[que[l]+1].a;
        while(l<r&&cal(que[r],i)<cal(que[r-1],que[r]))r--;
        que[++r]=i;
    }
    printf("%lld",f[tot]);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值