题意

题目链接

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. MYSQL NULL值特性

    NULL是一种“没有类型”的值,通常表示“无值”,“未知值”,“缺失值”,“超界”,“不在其中”等,我们在日常运用中很容易和NULL字符串混淆,这里大致整理了下NULL值的一些特性,以便能够正确使用N ...

  2. git aliases

    单独的 alias git config --global alias.co checkout git config --global alias.br branch git config --glo ...

  3. Centos7下安装zabbix 3.0.19

    参考网站: https://www.cnblogs.com/xiewenming/p/7732144.html https://www.cnblogs.com/clsn/p/7885990.html  ...

  4. Oracle sys或者system的默认密码

    Oracle的sys和system默认密码   system默认:manager sys默认:change_on_install 使用SQL Plus登录数据库时,system使用密码manager可 ...

  5. 课程一(Neural Networks and Deep Learning),第一周(Introduction to Deep Learning)—— 1、经常提及的问题

    Frequently Asked Questions Congratulations to be part of the first class of the Deep Learning Specia ...

  6. (转)MySQL中show语法

    MySQL中show语法 1. show tables或show tables from database_name; -- 显示当前数据库中所有表的名称. 2. show databases; -- ...

  7. ElasticSearch-SQL 安装和使用

    ES上线之后,用lucene语法来查询数据,学习成本略高,所以考虑用es-sql来简化这部分的操作. ES版本:5.4.0,节点部署如下: master node:3 client node:2,po ...

  8. Spring Security构建Rest服务-1204-Spring Security OAuth开发APP认证框架之Token处理

    token处理之一基本参数配置 处理token时间.存储策略,客户端配置等 以前的都是spring security oauth默认的token生成策略,token默认在org.springframe ...

  9. Zookeeper+ActiveMQ集群搭建

    搭建三台虚拟机安装centos7.要提前安装好jdk环境 1.环境准备,搭建三台虚拟机ip分别是 192.168.192.130 192.168.192.131 192.168.192.134 Zoo ...

  10. Oracle 相关查询

    --创建用户 create user zzg identified by zzg123; --修改用户的密码 alter user zzg identified by unis; --所有用户所在的表 ...