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的更多相关文章

  1. CF 319C - Kalila and Dimna in the Logging Industry 斜率优化DP

    题目:伐木工人用电锯伐木,一共需要砍n棵树,每棵树的高度为a[i],每次砍伐只能砍1单位高度,之后需要对电锯进行充电,费用为当前砍掉的树中最大id的b[id]值.a[1] = 1 , b[n] = 0 ...

  2. 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 ...

  3. Codeforces Round #189 (Div. 1 + Div. 2)

    A. Magic Numbers 不能出现连续的3个4,以及1.4以外的数字. B. Ping-Pong (Easy Version) 暴力. C. Malek Dance Club 考虑\(x\)二 ...

  4. 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 ...

  5. Codeforces Round #189 (Div. 2)

    题目地址:http://codeforces.com/contest/320 第一题:基本题,判断mod 1000,mod 100.,mod 10是不是等于144.14.1,直到为0 代码如下: #i ...

  6. Codeforces Round #189 (Div. 2) A. Magic Numbers

    #include <iostream> #include <vector> #include <algorithm> #include <string> ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. MT【95】由参数前系数凑配系数题2

    提示:都是看$a,b$前的系数做的$a=4/3,b=2/3;a+b=\le2$,一样的可以求得$a+b$的最小值-1,当$b=\frac{1}{3},a=\frac{-4}{3}$时取到等号.此题是清 ...

  2. 【题解】 [ZJOI2012]灾难 (拓扑排序+LCA)

    懒得复制,戳我戳我 Solution: 这题思路很神奇,首先你要知道这个毁灭树是怎么保证实现的:一句话就是如果该节点要被破坏,他的所有父节点就要被破坏,也就只要所有父节点的LCA被破坏就可以,所以我们 ...

  3. 《Migrating to Cloud-Native Application Architectures》学习笔记之Chapter 2. Changes Needed 原

    Cultural Change 文化变革 A great deal of the changes necessary for enterprise IT shops to adopt cloud-na ...

  4. 【BZOJ1489】[HNOI2009]双递增序列(动态规划)

    [BZOJ1489][HNOI2009]双递增序列(动态规划) 题面 BZOJ 洛谷 题解 这\(dp\)奇奇怪怪的,设\(f[i][j]\)表示前\(i\)个数中,第一个数列选了\(j\)个数,第二 ...

  5. VirtualBox 文件共享及网络配置技巧

    文件共享   1. 安装增强功能 2. 分配数据空间 这样就可以在virtualbox里访问本地的数据了:   网络配置

  6. 【洛谷P2661】信息传递 (updated)

    题目大意:给定一棵 N 个节点的内向树森林,求该内向树森林的最小环的大小(按边计算). 题解:先删链,再计算环的大小,统计答案即可. 代码如下 #include <bits/stdc++.h&g ...

  7. SQL Server - 开窗函数

    -- 开窗函数:在结果集的基础上进一步处理(聚合操作) SELECT * FROM dbo.Student S# Sname Sage Ssex ---------- ---------- ----- ...

  8. 基于 Express 搭建一个node项目 - 起步

    一,如何基于 Express 搭建一个node项目 什么是Express 借用官方的介绍,Express是一个基于Node.js平台的极简.灵活的web应用开发框架,它提供了一系列强大的特性,帮助你创 ...

  9. HDU 3595 every-sg模型

    多个子游戏同时进行,每个子游戏给出两个数a,b,可以将大的数减去k倍小的数,不能操作者输. 策略就是对于一个必胜的游戏要使得步数更长,对于一个必败的游戏使得步数最短. 以下都来自贾志豪的论文.. 对于 ...

  10. NP难问题求解综述

    NP难问题求解综述 摘要:定义NP问题及P类问题,并介绍一些常见的NP问题,以及NP问题的一些求解方法,最后最NP问题求解的发展方向做一些展望.   关键词:NP难问题 P类问题 算法 最优化问题   ...