题意

题目链接

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 斜率优化)的更多相关文章

  1. bzoj1597: [Usaco2008 Mar]土地购买 dp斜率优化

    东风吹战鼓擂第一题土地购买送温暖 ★★★   输入文件:acquire.in   输出文件:acquire.out   简单对比时间限制:1 s   内存限制:128 MB 农夫John准备扩大他的农 ...

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

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

  3. 1597: [Usaco2008 Mar]土地购买 [ dp+斜率优化 ] 未完

    传送门 1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1979  Solved: 705[Subm ...

  4. 2018.09.10 bzoj1597: [Usaco2008 Mar]土地购买(斜率优化dp)

    传送门 终究还是通宵了啊... 这是一道简单的斜率优化dp. 先对所有土地排序,显然如果有严格小于的两块土地不用考虑小的一块. 于是剩下的土地有一条边单增,另外一条单减. 我们假设a[i]是单减的,b ...

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

    Description 农夫John准备扩大他的农场,他正在考虑N (1 <= N <= 50,000) 块长方形的土地. 每块土地的长宽满足(1 <= 宽 <= 1,000, ...

  6. [bzoj1597][usaco2008 mar]土地购买 (动态规划+斜率优化)

    Description 农夫John准备扩大他的农场,他正在考虑N (1 <= N <= 50,000) 块长方形的土地. 每块土地的长宽满足(1 <= 宽 <= 1,000, ...

  7. 【BZOJ 1597】 [Usaco2008 Mar]土地购买 (斜率优化)

    1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3601  Solved: 1322 Descrip ...

  8. BZOJ 1597: [Usaco2008 Mar]土地购买【斜率优化+凸包维护】

    1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4989  Solved: 1847[Submit] ...

  9. BZOJ 1597 [Usaco2008 Mar]土地购买:斜率优化dp

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1597 题意: 有n块矩形土地,长为a[i],宽为b[i]. FJ想要将这n块土地全部买下来 ...

随机推荐

  1. powerDesiner设计数据库的一些用法

    数据库的设计主要有以下几个步骤: 1:需求分析:根据业务需求分析出满足客户的需求,从而建立相应的数据库 2:概念设计:通过数据抽象,设计系统概念模型,一般为E-R模型:(entity-relation ...

  2. 使用 spring.profiles.active 及 @profile 注解 动态化配置内部及外部配置

    引言:使用 spring.profiles.active 参数,搭配@Profile注解,可以实现不同环境下(开发.测试.生产)配置参数的切换 一.根据springboot的配置文件命名约定,结合ac ...

  3. HttpClient和HttpURLConnection的使用和区别(上)

    转自:点击打开链接 相信很多Android开发者碰到涉及到Http协议的需求时,都和我一样在犹豫是使用HttpClient还是使用HttpURLConnection呢.我在网上也搜索了很多文章,来分析 ...

  4. 【codeforces 24D】损坏的机器人

    题目大意:有一只坏了的机器人站在一个n∗m的网格里,初始位置在(x,y).现在每个单位时间内它会随机选左右下三个方向走,如果它随机的方向会走出网格就不会往这个方向走.当然这个机器人也可能原地停留一个单 ...

  5. (转)MySQL 加锁处理分析

    MySQL 加锁处理分析 原文:http://hedengcheng.com/?p=771 1    背景    1 1.1    MVCC:Snapshot Read vs Current Read ...

  6. 前端必备:FastStoneCapture 和 Licecap &&& mingw c++ 编译执行

    端必备:FastStoneCapture 和 Licecap FastStoneCapture这个软件非常小,只有2M多,并且其功能很强大,包括截图,录制视频,量尺,取色等等,对于前端工程师绝对是必备 ...

  7. Vue的实时时间转换Demo

    Vue的实时时间转换Demo time.html: <!DOCTYPE html> <html lang="en"> <head> <me ...

  8. Spark源码的编译过程详细解读(各版本)(博主推荐)

    不多说,直接上干货! 说在前面的话   重新试多几次.编译过程中会出现下载某个包的时间太久,这是由于连接网站的过程中会出现假死,按ctrl+c,重新运行编译命令.  如果出现缺少了某个文件的情况,则要 ...

  9. Python&Appium实现安卓手机图形解锁

    首先,在解锁状态下,建立一个Session,打开APP.然后,调用press_keycode()方法传入整型数值"26",锁定屏幕.通过implicitly_wait()方法等待两 ...

  10. windows下搭建Cordova安卓环境

    phoneGap 被收购后衍生出了 Cordova Android 的开发工具也由 Eclipse 向 Android Studio 靠拢 行业动态在更新,所以资料也要跟着变化... 在开始之前,作为 ...