BZOJ1597: [Usaco2008 Mar]土地购买(dp 斜率优化)
题意
Sol
重新看了一遍斜率优化,感觉又有了一些新的认识。
首先把土地按照\((w, h)\)排序,用单调栈处理出每个位置第向左第一个比他大的位置,显然这中间的元素是没用的
设\(f[i]\)表示买了前\(i\)块土地的最小花费
\(f[i] = min_{j = 0}^{i - 1}(f[j] + w[i] * h[j + 1])\)
斜率优化一下
// luogu-judger-enable-o2
#include<bits/stdc++.h>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define int long long
#define LL long long
#define pt(x) printf("%d ", x);
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 1e6 + 10, mod = 1e9 + 7;
LL INF = 6e18 + 10;
const double eps = 1e-9;
template <typename Y> inline void chmin(Y &a, Y b){a = (a < b ? a : b);}
template <typename Y> inline void chmax(Y &a, Y b){a = (a > b ? a : b);}
template <typename Y> inline void debug(Y a){cout << a << '\n';}
int sqr(int x) {return x * x;}
int add(int x, int y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
void add2(int &x, int y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
int mul(int x, int y) {return 1ll * x * y % mod;}
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, cur, q[MAXN];
LL f[MAXN];
struct Node {
int w, h;
bool operator < (const Node &rhs) const {
return w == rhs.w ? h < rhs.h : w < rhs.w;
}
}a[MAXN];
double Y(int x) {
return f[x];
}
double X(int x) {
return -a[x + 1].h;
}
double slope(int a, int b) {
return double(Y(b) - Y(a)) / (X(b) - X(a));
}
signed main() {
N = read();
for(int i = 1; i <= N; i++) a[i].w = read(), a[i].h = read();
sort(a + 1, a + N + 1);
for(int i = 1; i <= N; i++) {
while(cur && a[i].h >= a[cur].h) cur--;
a[++cur] = a[i];
}
for(int i = 1; i <= cur; i++) f[i] = INF;
q[1] = 0;
for(int i = 1, h = 1, t = 1; i <= cur; i++) {
while(h < t && slope(q[h], q[h + 1]) < a[i].w) h++;
f[i] = (LL) f[q[h]] + 1ll * a[i].w * a[q[h] + 1].h;
while(h < t && slope(q[t], i) < slope(q[t - 1], q[t])) t--;
q[++t] = i;
}
cout << f[cur];
return 0;
}
BZOJ1597: [Usaco2008 Mar]土地购买(dp 斜率优化)的更多相关文章
- bzoj1597: [Usaco2008 Mar]土地购买 dp斜率优化
东风吹战鼓擂第一题土地购买送温暖 ★★★ 输入文件:acquire.in 输出文件:acquire.out 简单对比时间限制:1 s 内存限制:128 MB 农夫John准备扩大他的农 ...
- BZOJ 1597: [Usaco2008 Mar]土地购买( dp + 斜率优化 )
既然每块都要买, 那么一块土地被另一块包含就可以不考虑. 先按长排序, 去掉不考虑的土地, 剩下的土地长x递增, 宽y递减 dp(v) = min{ dp(p)+xv*yp+1 } 假设dp(v)由i ...
- 1597: [Usaco2008 Mar]土地购买 [ dp+斜率优化 ] 未完
传送门 1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1979 Solved: 705[Subm ...
- 2018.09.10 bzoj1597: [Usaco2008 Mar]土地购买(斜率优化dp)
传送门 终究还是通宵了啊... 这是一道简单的斜率优化dp. 先对所有土地排序,显然如果有严格小于的两块土地不用考虑小的一块. 于是剩下的土地有一条边单增,另外一条单减. 我们假设a[i]是单减的,b ...
- [BZOJ1597][Usaco2008 Mar]土地购买(斜率优化)
Description 农夫John准备扩大他的农场,他正在考虑N (1 <= N <= 50,000) 块长方形的土地. 每块土地的长宽满足(1 <= 宽 <= 1,000, ...
- [bzoj1597][usaco2008 mar]土地购买 (动态规划+斜率优化)
Description 农夫John准备扩大他的农场,他正在考虑N (1 <= N <= 50,000) 块长方形的土地. 每块土地的长宽满足(1 <= 宽 <= 1,000, ...
- 【BZOJ 1597】 [Usaco2008 Mar]土地购买 (斜率优化)
1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3601 Solved: 1322 Descrip ...
- BZOJ 1597: [Usaco2008 Mar]土地购买【斜率优化+凸包维护】
1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4989 Solved: 1847[Submit] ...
- BZOJ 1597 [Usaco2008 Mar]土地购买:斜率优化dp
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1597 题意: 有n块矩形土地,长为a[i],宽为b[i]. FJ想要将这n块土地全部买下来 ...
随机推荐
- Window Navigator对象
Window Navigator对象 txt = "<p>浏览器代号: " + navigator.appCodeName + "</p>&quo ...
- 3. C++ POD类型
POD全称Plain Old Data,通常用于说明1个类型的属性.通俗的讲,一个类或结构体通过二进制拷贝后还能保持其数据不变,那么它就是一个POD类型. C++11将POD划分为2个基本概念的合集, ...
- (转)Windows下MySQL :GUI安装和使用(MySQL GUI tools)
原文:http://blog.csdn.net/dahunbi/article/details/52970815 MySQL GUI Tools是MySQL官方提供的图形化管理工具,功能很强大,值得推 ...
- HTML编码规范 - 1
用两个空格来代替制表符(tab) -- 这是唯一能保证在所有环境下获得一致展现的方法. 嵌套元素应当缩进一次(即两个空格). 对于属性的定义,确保全部使用双引号,绝不要使用单引号. 不要在自闭和(se ...
- Android输入控件EditText和软键盘监听
1. 跳转到新的页面自动软键盘显示情况: 在配置清单文件AndroidManifest.xml文件,对Activity的windowSoftInputMode属性进行设置. stateUnspecif ...
- 软件魔方制作系统启动盘并安装win10系统
不多说,直接上干货! 推荐软件:软件魔方 http://mofang.ruanmei.com/ 这里,我想说的是,这个软件来制作系统盘,是真的方便和好处多多.具体我不多说,本人也是用过其他的如大白菜 ...
- Annotate类
在Annotate类中有个Annotator接口,定义如下: /** A client that has annotations to add registers an annotator, * th ...
- windows设置多用户模式
在实际使用我们较多使用的都是windows系统的单用户模式,它意味着当我们登录一个用户的时候如果另外一个用户也在登录,那么就得等待另外一个用户退出后才可以登录我们这个用户,但是实际需求中,我们经常会遇 ...
- Hive use mapreduce引擎 bsonFile splits报错处理
Hive use mapreduce引擎 1. 出现报错.关于bsonFile的.禁止hdfs里面存储的bson文件自动splits. 可以在hive-site.xml文件里面添加如 ...
- VUE自定义指令生命周期,VUE生命周期
一.自定义指令的生命周期 自定义指令有五个生命周期(也叫钩子函数),分别是 bind,inserted,update,componentUpdated,unbind bind:只调用一次,指令第一次绑 ...