题目描述

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

约翰准备扩大他的农场,眼前他正在考虑购买N块长方形的土地。如果约翰单买一块土 地,价格就是土地的面积。但他可以选择并购一组土地,并购的价格为这些土地中最大的长 乘以最大的宽。比如约翰并购一块3 × 5和一块5 × 3的土地,他只需要支付5 × 5 = 25元, 比单买合算。 约翰希望买下所有的土地。他发现,将这些土地分成不同的小组来并购可以节省经费。 给定每份土地的尺寸,请你帮助他计算购买所有土地所需的最小费用。

输入输出格式

输入格式:

* 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.

输入输出样例

输入样例#1: 复制

4
100 1
15 15
20 5
1 100

输出样例#1: 复制

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.






首先按高度递减,否则宽度递增来排序,这样可以去掉被某一个阵地包含的无用阵地

这样处理完之后,可以发现剩下的阵地有一个特点,就是高度递减,宽度递增,

于是不难想象,最有的方法是选连续的阵地,所以dp方程很好写:

然后这题的斜率项是一个正数,貌似图跟一般的不一样啊。。。

但是靠j优于k的式子搞就完事了





 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int N=100010;
 5 inline ll read();
 6 inline void write(ll x);
 7 inline void writeln(ll x);
 8
 9 int n,cnt=0;
10 int q[N],head=1,tail=0;
11 ll dp[N];
12 struct aa{
13     ll x,y;
14 } field[N],work[N];
15 inline bool operator<(const aa &a,const aa &b)
16 {
17     if(a.x!=b.x) return a.x<b.x;
18     return a.y<b.y;
19 }
20 #define Y(i) (dp[(i)])
21 #define X(i) (-work[(i)+1].y)
22 #define Slope(i,j) 1.0*(Y(i)-Y(j))/(X(i)-X(j))
23 #define calc(i,j) (dp[(j)]+work[i].x*work[j+1].y)
24
25 inline ll read()
26 {
27     ll s=0;
28     bool flag=false;
29     char ch=getchar();
30     for(;ch<'0'||ch>'9';ch=getchar()) if(ch=='-') flag=true;
31     for(;'0'<=ch&&ch<='9';ch=getchar()) s=(s<<3)+(s<<1)+(ch^48);
32     if(flag) return -s;
33     return s;
34 }
35 inline void write(ll x)
36 {
37     if(!x)
38     {
39         putchar('0'),putchar(' ');
40         return ;
41     }
42     if(x<0) putchar('-'),x=-x;
43     char ch[20];
44     int tot=0;
45     while(x) ch[++tot]=x%10,x/=10;
46     for(int i=tot;i;i--) putchar(ch[i]^48);
47     putchar(' ');
48 }
49 inline void writeln(ll x)
50 {
51     write(x);
52     putchar('\n');
53 }
54
55 int main()
56 {
57     n=read();
58     for(int i=1;i<=n;i++) field[i].x=read(),field[i].y=read();
59     sort(field+1,field+n+1);
60     for(int i=1;i<=n;i++)
61     {
62         while(cnt&&field[i].y>=work[cnt].y) cnt--;
63         work[++cnt]=field[i];
64     }
65     n=cnt;
66     memset(dp,0x3f,sizeof(dp));
67     dp[0]=0;
68     q[++tail]=0;
69     for(int i=1;i<=n;i++)
70     {
71         for(;head<tail&&Slope(q[head],q[head+1])<=work[i].x;head++);
72         dp[i]=calc(i,q[head]);
73         for(;head<tail&&Slope(q[tail-1],q[tail])>=Slope(q[tail],i);tail--);
74         q[++tail]=i;
75     }
76     writeln(dp[n]);
77     return 0;
78 }

转载于:https://www.cnblogs.com/zhangbuang/p/10617204.html

P2900 [USACO08MAR]土地征用Land Acquisition(斜率优化)相关推荐

  1. [USACO08MAR]土地征用Land Acquisition

    题面在这里 题意 约翰准备扩大他的农场,眼前他正在考虑购买N块长方形的土地. 如果约翰单买一块土地,价格就是土地的面积,但他可以选择并购一组土地, 并购的价格为这些土地中最大的长乘以最大的宽. 给定每 ...

  2. 『摆渡车 斜率优化dp及总结』

    摆渡车的题解我已经写过一遍了,在这里,这次主要从斜率优化的角度讲一下摆渡车,并总结一下斜率优化会出现的一些奇奇怪怪的错误. 摆渡车 Description 有 n 名同学要乘坐摆渡车从人大附中前往人民 ...

  3. P2900-[USACO08MAR]Land AcquisitionG【斜率优化】

    正题 题目链接:https://www.luogu.com.cn/problem/P2900 题目大意 nnn块hi∗wih_i*w_ihi​∗wi​的土地,购买一组土地需要max(h)∗max(w) ...

  4. 【总结】斜率优化DP

    于是,XSC062开始写总结. 斜率优化DP 前置芝士 单调队列优化DP(夹带私货) 正文 我们以一道题为例. 打印文章 双倍经验 三倍经验 Solution 明显DP. 那么DP式就是: f i = ...

  5. BZOJ1597: [Usaco2008 Mar]土地购买(dp 斜率优化)

    题意 题目链接 Sol 重新看了一遍斜率优化,感觉又有了一些新的认识. 首先把土地按照\((w, h)\)排序,用单调栈处理出每个位置第向左第一个比他大的位置,显然这中间的元素是没用的 设\(f[i] ...

  6. BZOJ 1597: [Usaco2008 Mar]土地购买( dp + 斜率优化 )

    既然每块都要买, 那么一块土地被另一块包含就可以不考虑. 先按长排序, 去掉不考虑的土地, 剩下的土地长x递增, 宽y递减 dp(v) = min{ dp(p)+xv*yp+1 } 假设dp(v)由i ...

  7. 【BZOJ3963】[WF2011]MachineWorks cdq分治+斜率优化

    [BZOJ3963][WF2011]MachineWorks Description 你是任意性复杂机器公司(Arbitrarily Complex Machines, ACM)的经理,公司使用更加先 ...

  8. BZOJ 1492: [NOI2007]货币兑换Cash [CDQ分治 斜率优化DP]

    传送门 题意:不想写... 扔链接就跑 好吧我回来了 首先发现每次兑换一定是全部兑换,因为你兑换说明有利可图,是为了后面的某一天两种卷的汇率差别明显而兑换 那么一定拿全利啊,一定比多天的组合好 $f[ ...

  9. 『玩具装箱TOY 斜率优化DP』

    玩具装箱TOY(HNOI2008) Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊 ...

最新文章

  1. 《C#多线程编程实战(原书第2版)》——第3章 使用线程池 3.1 简介
  2. HDU 3625 Examining the Rooms【第一类斯特灵数】
  3. apply筛选 pandas_更快的pandas.apply搜索方法
  4. 音频管理_人力资源管理师考试历年真题试卷+视频教程+音频讲义合集分享
  5. 如何用Seaborn描绘柱状图(条形图),箱线图,小提琴图,分类散点图,分面网格分类图,散点图(3)
  6. oracle数据库分层,Oracle数据库的分层查询(一)
  7. 历届试题 打印十字图
  8. vfs管理下的linux文件系统
  9. Flash ios 开发cookbook 下载(PDF)
  10. 使用Spring-Retry重试处理
  11. WebBrowser,挖坑,跳坑,填坑
  12. C文件 写入 读取 函数
  13. html5怎么在画布怎么旋转,javascript – 如何旋转HTML5画布的现有内容?
  14. python小测验3_python基础小测试
  15. 分析匿名页(anonymous_page)映射
  16. android9 三星 港版,三星S10+官方港版安卓9完整固件升级更新包:TGY-G9750ZHU1ASF1
  17. 如何查看win10系统的激活情况
  18. 根据图片地址检查图片格式,今日头条有效
  19. 什么是Java SDK
  20. spring学习笔记之配置文件applicationContext.xml

热门文章

  1. jspm彩虹滑板专卖网店系统毕业设计(附源码、运行环境)
  2. 时序数据库为万物互联打下坚实的基石
  3. python将矩阵顺时针旋转90度_在Python中将方形矩阵逆时针旋转90度的程序
  4. 使用Scrum进行敏捷项目管理的10个简单步骤
  5. 什么是等保三级?等保三级的认证流程有哪些?
  6. 跟风用Matlab画一棵圣诞树
  7. vue高德地图marker批量标记与InfoWindow提示框
  8. 数据结构——王卓老师
  9. 三维视频融合技术.在公共安全领域中的应用
  10. DeepLearing:GAN生成式对抗网络