【洛谷 P2900】 [USACO08MAR]土地征用Land Acquisition(斜率优化,单调栈)
2024-08-31 00:25:44
题目链接
双倍经验
设\(H\)表示长,\(W\)表示宽。
若\(H_i<H_j\)且\(W_i<W_j\),显然\(i\)对答案没有贡献。
于是把所有点按\(H\)排序,然后依次加入一个按\(W\)降序排序的单调栈。
这个单调栈里就是一定对答案有贡献的点,现在的问题就是把这些点分段,使总费用最小。
设\(f[i]\)表示前\(i\)块土地的最小费用。
然后枚举断点\(0<=j<i\),则\(f[i]=\min(f[j]+W_{j+1}*H_i)\)
斜率优化搞一搞就行了。
// f[i] = f[j] + x[i] * y[j + 1]
// f[j] = -x[i] * y[j + 1] - f[i]
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 50010;
#define ll long long
inline ll min(const ll a, const ll b){
return a < b ? a : b;
}
int n, top;
struct point{
int x, y;
int operator < (const point A) const{
return x != A.x ? x < A.x : y < A.y;
}
}a[MAXN], st[MAXN];
int q[MAXN], head, tail;
ll f[MAXN];
inline double k(int i, int j){
return (f[i] - f[j]) / (st[i + 1].y - st[j + 1].y);
}
int main(){
scanf("%d", &n);
for(int i = 1; i <= n; ++i)
scanf("%d%d", &a[i].x, &a[i].y);
sort(a + 1, a + n + 1);
for(int i = 1; i <= n; ++i){
while(top && st[top].y <= a[i].y) --top;
st[++top] = a[i];
}
for(int i = 1; i <= top; ++i){
while(head < tail && k(q[head], q[head + 1]) > -st[i].x) ++head;
int j = q[head];
f[i] = f[j] + (ll)st[i].x * st[j + 1].y;
while(head < tail && k(q[tail - 1], q[tail]) <= k(q[tail], i)) --tail;
q[++tail] = i;
}
printf("%lld\n", f[top]);
return 0;
}
最新文章
- PHP的输出缓冲区(转)
- 大数据系列(3)——Hadoop集群完全分布式坏境搭建
- [ZigBee] 14、Zigbee无线通信前奏——BasicRF 简单无线点对点传输协议
- linux_shell_4_shell特性
- 云服务器 ECS Linux 系统盘数据转移方法
- while DEMO
- Object-C中需要注意的小细节
- Zookeeper 脑裂
- 04.Hibernate一对一关联
- POJ 3243 Clever Y (求解高次同余方程A^x=B(mod C) Baby Step Giant Step算法)
- 统计nginx日志里流量
- 配置Ubuntu开发环境
- 浙大PTA - - File Transfer
- 详细比较三个 CSS 预处理器(框架):Sass、LESS 和 Stylus
- javascript学习(9)——[设计模式]单例
- MVC EF 修改 封装类 通用泛型方法(一)
- Ant自动构建
- 32位机器的LowMemory
- Android 的自动化测试资源
- Linux mount BSD disk partition
热门文章
- ZOJ 2060 A-Fibonacci Again
- 小程序解密 encryptedData 获取 unionID 等信息
- java和mysql的length()区别及char_length()
- Java中TimeZone类的常用方法
- Javascript中判断变量是数组还是对象(array还是object)
- Appium与Robotium区别
- DP——P2300 合并神犇
- [HNOI2014]江南乐 博弈论
- 【JavaScript】BOM
- tomcat7.x远程命令执行(CVE-2017-12615)漏洞漏洞复现