思路:先把没有用的土地去掉,然后按照x轴排序,容易得到dp转移方程

dp[ i ] = min{ dp[ j ] + b[ j + 1 ] * a[ i ] }    0 <= j < i

典型的斜率优化。

#include<bits/stdc++.h>
#define LL long long
#define ll long long
#define fi first
#define se second
#define mk make_pair
#define PII pair<int, int>
#define y1 skldjfskldjg
#define y2 skldfjsklejg using namespace std; const int N = 2e5 + ;
const int M = 1e7 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 +; struct node {
LL x, y;
node(LL x = , LL y = ) {
this->x = x;
this->y = y;
}
bool operator < (const node &rhs) const {
if(x == rhs.x) return y < rhs.y;
return x < rhs.x;
}
} a[N]; int n, tot, head, rear, stk[N];
LL dp[N]; bool check1(int id1, int id2, LL c) {
return dp[id2] - dp[id1] <= c * (a[id1 + ].y - a[id2 + ].y);
} bool check2(int id1, int id2, int id3, int id4) {
return (dp[id4] - dp[id3]) * (a[id1 + ].y - a[id2 + ].y) <= (dp[id2] - dp[id1]) * (a[id3 + ].y - a[id4 + ].y);
} int main() {
scanf("%d", &n);
for(int i = ; i <= n; i++) {
scanf("%lld%lld", &a[i].x, &a[i].y);
}
sort(a + , a + + n);
tot = ;
for(int i = ; i <= n; i++) {
while(tot && a[i].y >= a[tot].y) tot--;
a[++tot] = a[i];
}
n = tot; dp[] = ;
stk[++rear] = ; for(int i = ; i <= n; i++) {
while(rear > head && check1(stk[head], stk[head + ], a[i].x)) head++;
int id = stk[head];
dp[i] = dp[id] + a[id + ].y * a[i].x;
while(rear > head && check2(stk[rear - ], stk[rear], stk[rear], i)) rear--;
stk[++rear] = i;
}
printf("%lld\n", dp[n]);
return ;
} /*
*/

bzoj1597 斜率优化dp的更多相关文章

  1. BZOJ1597土地购买 【斜率优化DP】

    BZOJ1597土地购买 [斜率优化DP] Description 农夫John准备扩大他的农场,他正在考虑N (1 <= N <= 50,000) 块长方形的土地. 每块土地的长宽满足( ...

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

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

  3. 【BZOJ1597】【Usaco2008 Mar】土地购买 斜率优化DP

    题目: 题目在这里 思路与做法: 这题如果想要直接dp的话不太好处理. 不过, 我们发现如果\(a[i].x>=a[j].x\)且\(a[i].y>=a[j].y\) \((\)a是输入的 ...

  4. bzoj-4518 4518: [Sdoi2016]征途(斜率优化dp)

    题目链接: 4518: [Sdoi2016]征途 Description Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计划用m天到达T地 ...

  5. bzoj-1096 1096: [ZJOI2007]仓库建设(斜率优化dp)

    题目链接: 1096: [ZJOI2007]仓库建设 Description L公司有N个工厂,由高到底分布在一座山上.如图所示,工厂1在山顶,工厂N在山脚.由于这座山处于高原内陆地区(干燥少雨),L ...

  6. [BZOJ3156]防御准备(斜率优化DP)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3156 分析: 简单的斜率优化DP

  7. 【BZOJ-1096】仓库建设 斜率优化DP

    1096: [ZJOI2007]仓库建设 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3719  Solved: 1633[Submit][Stat ...

  8. BZOJ 1010: [HNOI2008]玩具装箱toy 斜率优化DP

    1010: [HNOI2008]玩具装箱toy Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再 ...

  9. BZOJ 3156: 防御准备 斜率优化DP

    3156: 防御准备 Description   Input 第一行为一个整数N表示战线的总长度. 第二行N个整数,第i个整数表示在位置i放置守卫塔的花费Ai. Output 共一个整数,表示最小的战 ...

随机推荐

  1. bzoj 2434 [Noi2011]阿狸的打字机 AC自动机

    [Noi2011]阿狸的打字机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 4001  Solved: 2198[Submit][Status][D ...

  2. How to Disable System Integrity Protection (rootless) in OS X El Capitan

    mac在10.11之后增加了一个功能,号称"System Integrity Protection, often called rootless",有了这个功能,以下目录的东西都不 ...

  3. [洛谷P4774] [NOI2018]屠龙勇士

    洛谷题目链接:[NOI2018]屠龙勇士 因为markdown复制过来有点炸格式,所以看题目请戳上面. 题解: 因为杀死一条龙的条件是在攻击\(x\)次,龙恢复\(y\)次血量\((y\in N^{* ...

  4. CharSequence 去除两端空格

    CharSequence就是字符序列,String, StringBuilder和StringBuffer都是其实现类. 模仿String.trim() 实现了一个CharSequence通用的去除两 ...

  5. 获取数据源数据的实现---Architecting Android

    UserRepository,这个接口,描述了Repository提供给用户的功能就是getUsers,getUser(ID).用户只管使用,其它细节无需理会. /** * Interface tha ...

  6. PHP正则 贪婪匹配与非贪婪匹配

    $str = ".abcdeabcde"; preg_match('/a.+?e/', $str, $match); print_r($match); Array ( [0] =& ...

  7. HDU 1026 Ignatius and the Princess I (广搜)

    题目链接 Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius ...

  8. HDU 2577 How to Type (字符串处理)

    题目链接 Problem Description Pirates have finished developing the typing software. He called Cathy to te ...

  9. 使用SQL Server连接xml接口,读取并解析数据

    --数据源格式,放到任意程序中部署接口即可--<Data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmln ...

  10. MapperScannerConfigurer不 property-placeholder

    关于org.mybatis.spring.mapper.MapperScannerConfigurer不支持 property-placeholder 参考了http://www.oschina.ne ...