zoukankan      html  css  js  c++  java
  • [BZOJ1579][Usaco2008 Mar]土地购买

    1597: [Usaco2008 Mar]土地购买

    Time Limit: 10 Sec  Memory Limit: 162 MB
    Submit: 5094  Solved: 1887
    [Submit][Status][Discuss]

    Description

    农夫John准备扩大他的农场,他正在考虑N (1 <= N <= 50,000) 块长方形的土地. 每块土地的长宽满足(1 <= 宽 <= 1,000,000; 1 <= 长 <= 1,000,000). 每块土地的价格是它的面积,但FJ可以同时购买多快土地. 这些土地的价格是它们最大的长乘以它们最大的宽, 但是土地的长宽不能交换. 如果FJ买一块3x5的地和一块5x3的地,则他需要付5x5=25. FJ希望买下所有的土地,但是他发现分组来买这些土地可以节省经费. 他需要你帮助他找到最小的经费.

    Input

    * 第1行: 一个数: N

    * 第2..N+1行: 第i+1行包含两个数,分别为第i块土地的长和宽

    Output

    * 第一行: 最小的可行费用.

    Sample Input

    4
    100 1
    15 15
    20 5
    1 100

    输入解释:

    共有4块土地.

    Sample Output

    500
     
    若一块土地x,y均不比另一块的大,那么可以顺便买了,对答案没有影响,去掉
    剩下的土地排成x增y减,设$dp[i]$表示买下前i块土地的最小花费,则$dp[i]=min(dp[j]+y[j+1]*x[i]) (0<=j<i)$,显然斜率优化
    #include <cstdio>
    #include <algorithm>
    using namespace std; 
    const int maxn = 50000 + 10;
    typedef long long ll;
    struct Node{
        int x, y;
        bool operator < (const Node &a) const {
            return x != a.x ? x < a.x : y < a.y;
        }    
    }no[maxn];
    int x[maxn], y[maxn], cnt;
    ll dp[maxn];
    double slope(int a, int b){
        return (double)(dp[a] - dp[b]) / (y[a + 1] - y[b + 1]);
    }
    int q[maxn], h, t;
    int main(){
        int n;
        scanf("%d", &n);
        for(int i = 1; i <= n; i++) scanf("%d %d", &no[i].x, &no[i].y);
        sort(no + 1, no + n + 1);
        cnt = 1;
        x[1] = no[1].x;
        y[1] = no[1].y;
        for(int i = 2; i <= n; i++){
            while(cnt && no[i].y >= y[cnt]) cnt--;
            cnt++;
            x[cnt] = no[i].x;
            y[cnt] = no[i].y;
        }
        dp[0] = 0;
        q[h = t = 1] = 0;
        for(int i = 1; i <= cnt; i++){
            while(h < t && slope(q[h], q[h + 1]) > -x[i]) h++;
            dp[i] = dp[q[h]] + (ll)y[q[h] + 1] * x[i];
            while(h < t && slope(q[t - 1], q[t]) < slope(q[t], i)) t--;
            q[++t] = i;
        }
        printf("%lld
    ", dp[cnt]);
        return 0;
    }
  • 相关阅读:
    小程序
    wepy
    html5 +css3 点击后水波纹扩散效果 兼容移动端
    vue+element 切换正式和测试环境
    Nuxt
    vue相关安装命令
    【react】--------------配置react项目根路径-------------【劉】
    【Android】--------------高版本http请求错误-------------【劉】
    【react-native】--------------检测SIM卡是否安装-------------【劉】
    【javascript】--------------http-------------【劉】
  • 原文地址:https://www.cnblogs.com/ruoruoruo/p/7467783.html
Copyright © 2011-2022 走看看