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. 【UVA514】铁轨

    题目大意:给定 N 个数,编号从 1 到 N,现需要判断,利用一个无限大的栈结构,能否实现到给定序列的转换. 题解:本题一共涉及三个部分的交互,分别是目标序列,栈和初始序列,由栈的顶端进入,顶端弹出性 ...

  2. Git基本知识

    一,安装 Ubuntu安装git:sudo apt-get install git-core Centos安装git:yum install git-core 二,配置身份---在提交代码时可以辨别身 ...

  3. C++ new动态数组初始化

    strlen函数是不包括‘\0’的长度的,sizeof计算的结果才包括'\0'的长度: C++ new动态数组初始化void testnew( const char* str ) { if (!str ...

  4. SQL Server 中ROW_NUMBER() OVER基本用法

    1.不能排序法 * FROM table1 WHERE id NOT IN ( SELECT TOP 开始的位置 id FROM table1 ) 2.SQL 2000 临时表法 DECLARE @S ...

  5. JavaScript事件代理和委托

    在javasript中,代理.委托经常出现. 那么它究竟在什么样的情况下使用?它的原理又是什么? 这里介绍一下javascript delegate的用法和原理,以及Dojo,jQuery等框架中de ...

  6. Linux quotacheck失败

    我找了多少个帖子才发现解决这个问题的啊...最终还是靠FQ找的这位大佬的文章  http://www.2daygeek.com/quotacheck-error/# 当我在执行quotacheck - ...

  7. [六字真言]4.叭.SpringMVC异常痛苦

    "叭",除畜生道劳役之苦: 在学过的三阶段的时候,我们对SpringMVC的异常处理,一直可以算是简单中透着暴力,不要不重视异常!真的很重要,不要让它处在尴尬的位置! 在二阶段或者 ...

  8. Spring Boot 源码分析 数据源 + Mybatis 配置

    公司今年开始使用 Spring Boot 开发,当然使用 Spring Boot 也是大势所趋,尤其是现在微服务的趋向,当然是选择基于Spring Boot 的 Spring Cloud.(所谓的 S ...

  9. JavaScript编写风格指南 (二)

    七:注释 // 频繁的使用注释有助于他人理解你的代码// 1.代码晦涩难懂// 2.可能被误认为是错误的代码// 3.必要但不明显的针对特定浏览器的代码// 4.对于对象,方法或者属性,生成文档是有必 ...

  10. ML—机器学习常用包(持续更新….)

    机器学习是计算机科学和统计学的边缘交叉领域,R关于机器学习的包主要包括以下几个方面: 1)神经网络(Neural Networks): nnet.AMORE以及neuralnet,nnet提供了最常见 ...