题目链接:http://codeforces.com/problemset/problem/463/B

题目意思:Caisa 站在 0 pylon 并且只有 0 energy,他需要依次跳过1 pylon、2 pylon,...直到最后的 n pylon,每个 pylon(第 i 个) 都有对应的 height 值 hi,而每当从第 i 个pylon 跳到第i+1个pylon的时候,energy会增加 hi-hi+1,当然这个增加值有可能是负数,此时的energy则会相应的减少,现在要确保 energy 在任何时刻都是一个非负数。Caisa 可以向任意的一个pylon 增加 height,每增加一个单元的 height就需要 1 dollar,问从第1个 pylon 跳到 第 n 个pylon,且energy 是 非负的情况下,需要的最少dollar是多少。

方法一:直接做,二分模拟(31ms)

 // 二分
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std; const int maxn = 1e5 + ;
int h[maxn], n; bool check(int x)
{
if (x - h[] < )
return false;
int energy = x - h[];
for (int i = ; i < n-; i++)
{
energy += h[i] - h[i+];
if (energy < )
return false;
}
return true;
} int main()
{
while (scanf("%d", &n) != EOF)
{
for (int i = ; i < n; i++)
scanf("%d", &h[i]);
int l = , r = maxn;
int ans = maxn;
while (l <= r)
{
int m = (l+r)>>;
if (check(m))
{
ans = min(ans, m);
r = m - ;
}
else
l = m + ;
}
printf("%d\n", ans);
}
return ;
}

方法二:

找出序列中的最大值即为答案(46ms,有点奇怪)。

因为任意一个pylon 和 这个最大值的差都为正数或者0(序列中有多个最大值),也就是energy 一定不会变为负数!!!次大值也是不行的,因为如果序列中 pylon 1 的值是负数,energy 就为负数了,不符合条件。

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; int main()
{
int n, h;
while (scanf("%d", &n) != EOF)
{
int ans = ;
for (int i = ; i < n; i++)
{
scanf("%d", &h);
ans = max(ans, h);
}
printf("%d\n", ans);
}
return ;
}

codeforces 463B Caisa and Pylons 解题报告的更多相关文章

  1. [CodeForces - 463B] Caisa and Pylons

    题目链接:http://codeforces.com/problemset/problem/463/B 求个最大值 AC代码: #include<cstdio> #include<c ...

  2. codeforces 463A Caisa and Sugar 解题报告

    题目链接:http://codeforces.com/problemset/problem/463/A 题目意思:某个人有 s dollar的钱,有 n 种类型的糖果,第 i 种糖果的价值为 xi d ...

  3. Codeforces Educational Round 92 赛后解题报告(A-G)

    Codeforces Educational Round 92 赛后解题报告 惨 huayucaiji 惨 A. LCM Problem 赛前:A题嘛,总归简单的咯 赛后:A题这种**题居然想了20m ...

  4. codeforces 476C.Dreamoon and Sums 解题报告

    题目链接:http://codeforces.com/problemset/problem/476/C 题目意思:给出两个数:a 和 b,要求算出 (x/b) / (x%b) == k,其中 k 的取 ...

  5. Codeforces Round #382 (Div. 2) 解题报告

    CF一如既往在深夜举行,我也一如既往在周三上午的C++课上进行了virtual participation.这次div2的题目除了E题都水的一塌糊涂,参赛时的E题最后也没有几个参赛者AC,排名又成为了 ...

  6. codeforces 507B. Amr and Pins 解题报告

    题目链接:http://codeforces.com/problemset/problem/507/B 题目意思:给出圆的半径,以及圆心坐标和最终圆心要到达的坐标位置.问最少步数是多少.移动见下图.( ...

  7. codeforces 500B.New Year Permutation 解题报告

    题目链接:http://codeforces.com/problemset/problem/500/B 题目意思:给出一个含有 n 个数的排列:p1, p2, ..., pn-1, pn.紧接着是一个 ...

  8. codeforces B. Xenia and Ringroad 解题报告

    题目链接:http://codeforces.com/problemset/problem/339/B 题目理解不难,这句是解题的关键 In order to complete the i-th ta ...

  9. codeforces 462C Appleman and Toastman 解题报告

    题目链接:http://codeforces.com/problemset/problem/461/A 题目意思:给出一群由 n 个数组成的集合你,依次循环执行两种操作: (1)每次Toastman得 ...

随机推荐

  1. ubuntu 配置 django

    安装 安装Apache sudo apt-get install apache2 安装Django 下载Django 安装mod_wsgi sudo apt-get install libapache ...

  2. ※版本管理※=>☆SVN工具=>※解决地域麻烦※№→搭建自己的网络SVN (SourceForge 免费) [转]

    源文 http://blog.csdn.net/xiaoting451292510/article/details/8562570 分类: 版本管理 2013-02-01 14:44 26057人阅读 ...

  3. 转: 多版本并发控制(MVCC)在分布式系统中的应用 (from coolshell)

    from:  http://coolshell.cn/articles/6790.html 问题 最近项目中遇到了一个分布式系统的并发控制问题.该问题可以抽象为:某分布式系统由一个数据中心D和若干业务 ...

  4. JMeter 压力測试使用函数和 CSV 文件參数化 json 数据

            在 http Load Testing 中.json 数据的提交是个让人头疼的问题.本文具体介绍怎样进行 JMeter 的 json 測试提交,以及怎样将其參数化.        St ...

  5. 使用Golang利用ectd实现一个分布式锁

    http://blog.codeg.cn/post/blog/2016-02-24-distrubute-lock-over-etcd/ By zieckey · 2016年02月24日 · 1205 ...

  6. springMVC学习之验证

    验证框中@NotEmpty.@NotBlank.@NotNull乍一看还是容易弄混的.主要使用情况记录一下: @NotEmpty 用在集合类上面 @NotBlank 用在String上面 @NotNu ...

  7. Node.js知识点学习

    Node.js知识点学习 一.基本概念 Node.js,或者 Node,是一个可以让 JavaScript 运行在服务器端的平台.可以说,Node.js开创了javascript模块化开发的先河,早期 ...

  8. C# trim split dataGrid

    C#中Trim().TrimStart().TrimEnd()的用法:     这三个方法用于删除字符串头尾出现的某些字符.Trim()删除字符串头部及尾部出现的空格,删除的过程为从外到内,直到碰到一 ...

  9. 笔记16 C# typeof() & GetType()

    C#中任何对象都具有GetType()方法,它的作用和typeof()相同,返回Type类型的当前对象的类型.typeof(x)中的x,必须是具体的类名.类型名称等,不可以是变量名称:GetType( ...

  10. make mrproper及mrproper的含义

    Linux下面去编译项目之前,一般常会用make mrproper去先删除之前编译所生成的文件和配置文件,备份文件等,其中,mrproper和distclean,clean之间的区别,Linux内核源 ...