题目链接: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. 关于编写Windows程序中启动兼容性问题

    之前用qt4编写Windows程序的时候遇到了一个软件在系统的兼容性问题:用户在win10系统下使用这个程序的时候,如果没有用低于win10版本的兼容模式运行的时候,存在运行某部分功能的时候无法使用的 ...

  2. xcode5 asset catalogs 由于图标尺寸错误导致编译问题解决[原创]

    如下图,即使图片尺寸不规范,xcode5也可以正常预览(这里我提供的尺寸是57*57, 而需要的是120*120) 但编译运行失败,报的错是: Images.xcassets: error: The ...

  3. 使用sqlalchemy查询并删除数据表的唯一性索引

    简单描述表结构,字段类型 desc tabl_name 删除索引:alter table `db`.`table_name` drop index `index_name`  注意里面的特殊符号: ` ...

  4. Git 常用场景操作

    git init      在本地新建一个repo,进入一个项目目录,执行git init,会初始化一个repo,并在当前文件夹下创建一个.git文件夹.   git clone      获取一个u ...

  5. sql的一些知识_通配符

    like操作符 通配符只能用于字符串查询 % 指任意字符出现任意次数,包括0次,不包括NULL SELECT username,weight,age FROM userinfo WHERE usern ...

  6. PS 基础知识 渐变编辑器如何使用

    ps渐变编辑器在哪 [ 标签:渐变,ps 渐变,编辑器 ] _______志 敏 回答:3 人气:9 解决时间:2009-04-16 15:28 满意答案 你先点渐变工具 然后左上出现渐变条设置 如图 ...

  7. Odoo event

    使用流程 建立活动 发布到网站 在线销售 订单确认,付款确认 注册.出席     建立活动         设置门票         确认并发布到网站     进入编辑模式,即可在线编辑活动     ...

  8. Golang Map Addressability

    http://wangzhezhe.github.io/blog/2016/01/22/golangmapaddressability-dot-md/ 在golang中关于map可达性的问题(addr ...

  9. 解题报告 之 HDU5288 OO&#39; s Sequence

    解题报告 之 HDU5288 OO' s Sequence Description OO has got a array A of size n ,defined a function f(l,r) ...

  10. JobConf

    /**  * A map/reduce job configuration. * 翻译:一个map/reduce作业配置 * <p><code>JobConf</code ...