P2900 [USACO08MAR]土地征用Land Acquisition (斜率优化dp)

题目描述

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.

输入输出样例


100 1 
15 15 
20 5 
1 100 
 

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块矩形土地,单买一块土地的花费等于该土地的面积,但是如果我们要买下一组土地,花费的钱等于该组土地中最大的长乘以最大的宽,clf(土豪)想要想买下所有土地,问至少需要多少钱?

题解:存在一些土地的长和宽都小于某块土地,所以我们可以优先去掉这些白给的土地,剩下的土地我们排序可以发现,当土地的长递增时,土地的宽必定递减。就很容易写出一个dp方程:

dp[i] = min\left \{ dp[j] + a[i].x * a[j+1].y \right \}  ,其中dp[i]表示前i块土地的最小花费,复杂度O(n^{2})

化为:dp[j] =- a[i].x * a[j+1].y + dp[i],斜率优化一下就OK了,复杂度O(n)

//#include
//#include
//#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define LL long long
#define ULL unsigned long long
#define MT(a, b) memset(a,b,sizeof(a))
#define lson l, mid, node<<1
#define rson mid + 1, r, node<<1|1
const int INF  =  0x3f3f3f3f;
const int O    =  1e6;
const int mod  =  1e9+7;
const int maxn =  2e5 +5;
const double PI  =  acos(-1.0);
const double E   =  2.718281828459;
const double eps = 1e-8;

struct dd {
    LL x,  y;
    bool friend operator < (dd a, dd b) {
        return a.x == b.x ? a.y < b.y : a.x < b.x;
    }
}a[maxn], b[maxn];

LL dp[maxn];

double get_k(int i, int j){
    return double(dp[j] - dp[i]) / double(a[j+1].y - a[i+1].y);
}

int main(){
    int n; cin >> n;
    for(int i=0; i=0; i--) {
        if(a[i].y <= b[cnt-1].y) continue;
        b[cnt ++] = a[i];
    }
    for(int i=0; i= get_k(q[r-1], q[r-2])) r --;
        q[r ++] = i;
    }
    cout << dp[cnt] <

 

你可能感兴趣的:(斜率优化dp)