zoukankan      html  css  js  c++  java
  • BZOJ 1597: [Usaco2008 Mar]土地购买【斜率优化+凸包维护】

    1597: [Usaco2008 Mar]土地购买

    Time Limit: 10 Sec  Memory Limit: 162 MB
    Submit: 4989  Solved: 1847
    [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

    HINT

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

    Source

    Gold

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1597

    分析:按照x,y递增排序,然后把可以和其它打包一起买的去掉,然后使得剩下一些y递减x递增的矩形。

    显然f[i]=min(f[j]+y[j+1]x[i]),然后再搞个斜率优化,方程是(f[j]-f[k])/(y[k+1]-y[j+1])<x[i],然后维护一个下凸包!

    下面给出AC代码:

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 inline int read()
     4 {
     5     int x=0,f=1;
     6     char ch=getchar();
     7     while(ch<'0'||ch>'9')
     8     {
     9         if(ch=='-')
    10             f=-1;
    11         ch=getchar();
    12     }
    13     while(ch>='0'&&ch<='9')
    14     {
    15         x=x*10+ch-'0';
    16         ch=getchar();
    17     }
    18     return x*f;
    19 }
    20 typedef long long ll;
    21 int n,tot;
    22 const int N=50050;
    23 ll x[N],y[N],f[N];
    24 int q[N];
    25 struct data
    26 {
    27     ll x,y;
    28 }a[N];
    29 inline bool cmp(data a,data b)
    30 {
    31     return a.x==b.x?a.y<b.y:a.x<b.x;
    32 }
    33 inline double slop(int a,int b)
    34 {
    35     return (double)(f[b]-f[a])/(y[a+1]-y[b+1]);
    36 }
    37 int main()
    38 {
    39     n=read();
    40     for(int i=1;i<=n;i++)
    41     {
    42         a[i].x=read();
    43         a[i].y=read();
    44     }
    45     sort(a+1,a+1+n,cmp);
    46     for(int i=1;i<=n;i++)
    47     {
    48         while(tot&&a[i].y>=y[tot])
    49             tot--;
    50         x[++tot]=a[i].x;
    51         y[tot]=a[i].y;
    52     }
    53     int l=0,r=0;
    54     for(int i=1;i<=tot;i++)
    55     {
    56         while(l<r&&slop(q[l],q[l+1])<x[i])
    57             l++;
    58         int t=q[l];
    59         f[i]=f[t]+y[t+1]*x[i];
    60         while(l<r&&slop(q[r],i)<slop(q[r-1],q[r]))
    61             r--;
    62         q[++r]=i;
    63     }
    64     printf("%lld
    ",f[tot]);
    65     return 0;
    66 }
  • 相关阅读:
    linux查看端口
    linux下Git代码
    linux安装mysql
    pip工具更新及解决"No module named pip"问题
    demo-bootstrap实现滑动开关
    vue 随笔
    css 解决盒子移动鼠标丢失产生的抖动问题
    笔记-纯css实现select的placeholder
    笔记-移动端rem适配和解决安卓手机键盘唤起引起样式问题
    demo-tab切换
  • 原文地址:https://www.cnblogs.com/ECJTUACM-873284962/p/7305264.html
Copyright © 2011-2022 走看看