bzoj 1597: [Usaco2008 Mar]土地购买(斜率优化)
2016-04-26 11:10
441 查看
1597: [Usaco2008 Mar]土地购买
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3069 Solved: 1143
[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
4100 1
15 15
20 5
1 100
输入解释:
共有4块土地.
Sample Output
500HINT
FJ分3组买这些土地: 第一组:100x1, 第二组1x100, 第三组20x5 和 15x15 plot. 每组的价格分别为100,100,300, 总共500.Source
Gold[Submit][Status][Discuss]
题解:斜率优化
把土地的长作为第一关键字,宽作为第二关键字,使长单调递增,宽单调递减
如果一个土地的长比另一块土地小,并且宽也比那一块土地小(或相等),那么这块土地对答案没有贡献,直接舍去。
这样就能保证长单调不下降,宽单调不上升
状态转移方程:f[i]=f[j]+x[i]*y[j]
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #define N 50003 #define ll long long using namespace std; int n,m,tot,q ,mark ; ll x ,y ,f ; struct data { ll x,y; };data a ; int cmp(data a,data b) { return a.x<b.x||a.x==b.x&&a.y>b.y; } ll K(int j) { return y[j+1]; } ll B(int j) { return f[j]; } ll calc(int i,int j) { return K(j)*x[i]+B(j); } bool pd(int x1,int x2,int x3) { ll w1=(K(x1)-K(x3))*(B(x2)-B(x1)); ll w2=(K(x1)-K(x2))*(B(x3)-B(x1)); return w1>=w2; } int main() { scanf("%d",&n); for (int i=1;i<=n;i++) scanf("%lld%lld",&a[i].x,&a[i].y); sort(a+1,a+n+1,cmp); tot=0; for (int i=1;i<=n;i++) { while (tot&&a[i].y>=y[tot]) tot--; tot++; x[tot]=a[i].x; y[tot]=a[i].y; } int head=0,tail=0; for (int i=1;i<=tot;i++) { while (head<tail&&calc(i,q[head])>=calc(i,q[head+1])) head++; f[i]=calc(i,q[head]); while (head<tail&&pd(i,q[tail-1],q[tail])) tail--; ++tail; q[tail]=i; } printf("%lld",f[tot]); }
相关文章推荐
- Emmet
- SQL SERVER 2014 Agent服务异常停止案例
- WebLogic Server 多租户资源迁移
- HTTP 错误 500.24 - Internal Server Error的解决方法
- TCP协议的三次握手和四次挥手
- 用Python做科学计算
- Freemarker资料
- appstore转让app
- <link rel="canonical" href="www.home.com">
- 多个图片文件夹生成一个大css文件的方法
- 简单的形态学处理流程
- XA,两阶段提交和X/Open协议
- About JAVA ,You Should Know-程序员的自我修养
- robotics toolbox 安装错误Second argument must be either "get" or "post“
- 配置wamp时局域网报错403无权限
- MySQL运行原理与基础架构
- 【转】TBF(Token Bucket Filter)
- IE8下Jquery获取select选中的值post到后台报错问题
- 无需 Flash 使用 jQuery 复制文字到剪贴板
- js中几种获得页面值的方式