Codeforces Round #189 (Div. 1) C - Kalila and Dimna in the Logging Industry 斜率优化dp
C - Kalila and Dimna in the Logging Industry
很容易能得到状态转移方程 dp[ i ] = min( dp[ j ] + b[ j ] * a[ i ] ), 然后斜率优化一下。
一直以为炸精度了, 忽然发现手贱把while 写成了if 。。。。
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long
using namespace std; const int N = 4e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-; int n, que[N], head, rear;
LL a[N], b[N], dp[N]; double calc(int k, int j) {
return 1.0 * (dp[j] - dp[k]) * (b[k] - b[j]);
} int main() {
scanf("%d", &n);
for(int i = ; i <= n; i++) scanf("%lld", &a[i]);
for(int i = ; i <= n; i++) scanf("%lld", &b[i]);
dp[] = ;
head = , rear = ;
que[++rear] = ;
for(int i = ; i <= n; i++) {
while(rear - head + >= && calc(que[head], que[head + ]) < a[i]) head++;
dp[i] = dp[que[head]] + b[que[head]] * a[i];
while(rear - head + >= && calc(que[rear-], que[rear]) >= calc(que[rear], i)) rear--;
que[++rear] = i;
}
printf("%lld\n", dp[n]);
return ;
} /*
*/
Codeforces Round #189 (Div. 1) C - Kalila and Dimna in the Logging Industry 斜率优化dp的更多相关文章
- CF 319C - Kalila and Dimna in the Logging Industry 斜率优化DP
题目:伐木工人用电锯伐木,一共需要砍n棵树,每棵树的高度为a[i],每次砍伐只能砍1单位高度,之后需要对电锯进行充电,费用为当前砍掉的树中最大id的b[id]值.a[1] = 1 , b[n] = 0 ...
- Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo 矩阵快速幂优化dp
E. Okabe and El Psy Kongroo time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Codeforces Round #189 (Div. 1 + Div. 2)
A. Magic Numbers 不能出现连续的3个4,以及1.4以外的数字. B. Ping-Pong (Easy Version) 暴力. C. Malek Dance Club 考虑\(x\)二 ...
- Codeforces Round #189 (Div. 1) B. Psychos in a Line 单调队列
B. Psychos in a Line Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/p ...
- Codeforces Round #189 (Div. 2)
题目地址:http://codeforces.com/contest/320 第一题:基本题,判断mod 1000,mod 100.,mod 10是不是等于144.14.1,直到为0 代码如下: #i ...
- Codeforces Round #189 (Div. 2) A. Magic Numbers
#include <iostream> #include <vector> #include <algorithm> #include <string> ...
- Codeforces Round #189 (Div. 2) D. Psychos in a Line 单调队列dp
D. Psychos in a Line time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #189 (Div. 2) A. Magic Numbers【正难则反/给出一个数字串判断是否只由1,14和144组成】
A. Magic Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Codeforces Round #527 (Div. 3) F. Tree with Maximum Cost 【DFS换根 || 树形dp】
传送门:http://codeforces.com/contest/1092/problem/F F. Tree with Maximum Cost time limit per test 2 sec ...
随机推荐
- 【CF949D】Curfew(贪心)
[CF949D]Curfew(贪心) 题面 CF 洛谷 破池姐姐翻译好强啊 题解 今天菊开讲这题,我大力猜想一波说肯定从中间有个分界线,他还说可能是假的 大力贪心就好了,从两边往中间考虑,只要这个房间 ...
- HGOI NOIP模拟4 题解
NOIP国庆模拟赛Day5 题解 T1 马里奥 题目描述 马里奥将要参加 NOIP 了,他现在在一片大陆上,这个大陆上有着许多浮空岛,并且其中一座浮空岛上有一个传送门,马里奥想要到达传送门从而前往 N ...
- 洛谷 P1073 最优贸易 解题报告
P1073 最优贸易 题目描述 \(C\)国有\(n\)个大城市和\(m\)条道路,每条道路连接这\(n\)个城市中的某两个城市.任意两个城市之间最多只有一条道路直接相连.这\(m\)条道路中有一部分 ...
- 【bzoj2440】 中山市选2011—完全平方数
http://www.lydsy.com/JudgeOnline/problem.php?id=2440 (题目链接) 题意 求第K个不含有完全平方因子的数 Solution 没想到莫比乌斯还可以用来 ...
- c++并发编程之线程的互斥与同步
什么是线程的同步与互斥? 互斥:指在某一时刻指允许一个进程运行其中的程序片,具有排他性和唯一性. 对于线程A和线程B来讲,在同一时刻,只允许一个线程对临界资源进行操作,即当A进入临界区对资源操作时,B ...
- vue 新增时清除表单验证注意事项
// 清除表单校验的提示 if (this.$refs['XXX']) { // 延时执行 this.$nextTick(function () { this.$refs['XXX'].clearVa ...
- jq禁用html标签
原文:http://www.jb51.net/article/105154.htm 移除或禁用html元素的点击事件可以通过css实现也可以通过js或jQuery实现. 一.CSS方法 .disabl ...
- java开发爬虫Deno
java开发爬虫Deno 身为一个程序员不会两三手爬虫怎么能在行业里立足啊,这是开发中自己写的一个java爬虫的Demo,供大家参考. java爬虫的开发依赖于jsoup.jar 直接上代码 publ ...
- SQL记录-PLSQL函数
PL/SQL函数 PL/SQL函数与过程相同,不同之处在于函数有一个返回值.因此,前面的章节中的所有讨论都适用于函数. 创建函数 建立一个独立函数可以使用CREATE FUNCTION语句创建.CRE ...
- Win10新增功能快捷键大全
原文地址:http://wenwen.sogou.com/z/q703976788.htm贴靠窗口:Win + 左/右 > Win + 上/下 > 窗口可以变为 1/4 大小放置在屏幕 4 ...