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

    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

    HINT

    FJ分3组买这些土地: 第一组:100x1, 第二组1x100, 第三组20x5 和 15x15 plot. 每组的价格分别为100,100,300, 总共500.

    正解:$DP$+斜率优化。

    我们可以发现,如果一个土地的长比另一个土地的长要小,宽也比它的宽要小,那么选这块土地作为最大土地肯定不会更优。于是我们可以直接把这块土地去掉。

    删除这类土地以后,我们把土地按照长度来排序。可以发现,土地此时长度递增,宽度递减。那么这就是一个裸的斜率优化$DP$了。

     1 //It is made by wfj_2048~
     2 #include <algorithm>
     3 #include <iostream>
     4 #include <complex>
     5 #include <cstring>
     6 #include <cstdlib>
     7 #include <cstdio>
     8 #include <vector>
     9 #include <cmath>
    10 #include <queue>
    11 #include <stack>
    12 #include <map>
    13 #include <set>
    14 #define inf (1<<30)
    15 #define il inline
    16 #define RG register
    17 #define ll long long
    18 #define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
    19 
    20 using namespace std;
    21 
    22 struct node{ ll a,b; }p[50010],s[50010];
    23 
    24 ll f[50010],q[50010],n,cnt,st,ed;
    25 
    26 il ll gi(){
    27     RG ll x=0,q=1; RG char ch=getchar();
    28     while ((ch<'0' || ch>'9') && ch!='-') ch=getchar();
    29     if (ch=='-') q=-1,ch=getchar();
    30     while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar();
    31     return q*x;
    32 }
    33 
    34 il ll cmp(const node &x,const node &y){
    35     if (x.a==y.a) return x.b<y.b; return x.a<y.a;
    36 }
    37 
    38 il double getk(RG ll i,RG ll j){
    39     return 1.0*(f[i]-f[j])/(s[i+1].b-s[j+1].b);
    40 }
    41 
    42 il void work(){
    43     n=gi(); for (RG ll i=1;i<=n;++i) p[i].a=gi(),p[i].b=gi();
    44     sort(p+1,p+n+1,cmp);
    45     for (RG ll i=1;i<=n;++i){
    46     while (cnt && s[cnt].a<=p[i].a && s[cnt].b<=p[i].b) cnt--;
    47     s[++cnt]=p[i];
    48     }
    49     s[cnt+1].b=1,st=ed=1;
    50     for (RG ll i=1;i<=cnt;++i){
    51     while (st<ed && getk(q[st],q[st+1])>=-s[i].a) st++;
    52     f[i]=f[q[st]]+s[i].a*s[q[st]+1].b;
    53     while (st<ed && getk(q[ed],q[ed-1])<=getk(q[ed],i)) ed--;
    54     q[++ed]=i;
    55     }
    56     printf("%lld
    ",f[cnt]); return;
    57 }
    58 
    59 int main(){
    60     File("buy");
    61     work();
    62     return 0;
    63 }
  • 相关阅读:
    To select the file to upload we can use the standard HTML input control of type
    Cascading Menu Script using Javascript Explained
    网站首页head区代码规范
    轻松掌握 Java 泛型
    JDK 5.0 中的泛型类型学习
    如何在firefox下获取下列框选中option的text
    是同步方法还是 synchronized 代码? 详解多线程同步规则
    javascript select option对象总结
    Select的动态取值(Text,value),添加,删除。兼容IE,FireFox
    javascript在ie和firefox下的一些差异
  • 原文地址:https://www.cnblogs.com/wfj2048/p/6707278.html
Copyright © 2011-2022 走看看