码迷,mamicode.com
首页 > 其他好文 > 详细

bzoj 1597

时间:2014-11-15 18:20:20      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:des   blog   http   io   ar   os   sp   for   div   

1597: [Usaco2008 Mar]土地购买

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 1685  Solved: 592
[Submit][Status]

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

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdlib>
using namespace std;
#define N 50010
struct arr{long long x,y;}a[N],b[N];
long long f[N],q[N],n,i,h,t,m;
bool cmp(arr a,arr b){return a.x<b.x||a.x==b.x&&a.y>b.y;}
int main()
{
  scanf("%lld",&n);
  for (i=1;i<=n;i++)
    scanf("%lld%lld",&b[i].x,&b[i].y);
  sort(b+1,b+n+1,cmp);
  m=1;a[1].x=b[1].x;a[1].y=b[1].y;
  for (i=2;i<=n;i++)
  {
    while (m&&b[i].y>a[m].y) m--;
    a[++m].x=b[i].x;a[m].y=b[i].y;
  }
  h=1;t=1;q[1]=0;f[0]=0;
  for (i=1;i<=m;i++)
  {
    while ((h<t)&&(a[i].x*(a[q[h]+1].y-a[q[h+1]+1].y)>f[q[h+1]]-f[q[h]])) h++;
    f[i]=f[q[h]]+a[q[h]+1].y*a[i].x;
    while ((h<t)&&(f[i]-f[q[t-1]])*(a[q[t]+1].y-a[i+1].y)>=(f[i]-f[q[t]])*(a[q[t-1]+1].y-a[i+1].y)) t--;
    q[++t]=i;
  }
  printf("%lld",f[m]);
  return 0;
}

  

bzoj 1597

标签:des   blog   http   io   ar   os   sp   for   div   

原文地址:http://www.cnblogs.com/a972290869/p/4099547.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!