codeforces 463B Caisa and Pylons 解题报告
题目链接: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 解题报告的更多相关文章
- [CodeForces - 463B] Caisa and Pylons
题目链接:http://codeforces.com/problemset/problem/463/B 求个最大值 AC代码: #include<cstdio> #include<c ...
- codeforces 463A Caisa and Sugar 解题报告
题目链接:http://codeforces.com/problemset/problem/463/A 题目意思:某个人有 s dollar的钱,有 n 种类型的糖果,第 i 种糖果的价值为 xi d ...
- Codeforces Educational Round 92 赛后解题报告(A-G)
Codeforces Educational Round 92 赛后解题报告 惨 huayucaiji 惨 A. LCM Problem 赛前:A题嘛,总归简单的咯 赛后:A题这种**题居然想了20m ...
- codeforces 476C.Dreamoon and Sums 解题报告
题目链接:http://codeforces.com/problemset/problem/476/C 题目意思:给出两个数:a 和 b,要求算出 (x/b) / (x%b) == k,其中 k 的取 ...
- Codeforces Round #382 (Div. 2) 解题报告
CF一如既往在深夜举行,我也一如既往在周三上午的C++课上进行了virtual participation.这次div2的题目除了E题都水的一塌糊涂,参赛时的E题最后也没有几个参赛者AC,排名又成为了 ...
- codeforces 507B. Amr and Pins 解题报告
题目链接:http://codeforces.com/problemset/problem/507/B 题目意思:给出圆的半径,以及圆心坐标和最终圆心要到达的坐标位置.问最少步数是多少.移动见下图.( ...
- codeforces 500B.New Year Permutation 解题报告
题目链接:http://codeforces.com/problemset/problem/500/B 题目意思:给出一个含有 n 个数的排列:p1, p2, ..., pn-1, pn.紧接着是一个 ...
- codeforces B. Xenia and Ringroad 解题报告
题目链接:http://codeforces.com/problemset/problem/339/B 题目理解不难,这句是解题的关键 In order to complete the i-th ta ...
- codeforces 462C Appleman and Toastman 解题报告
题目链接:http://codeforces.com/problemset/problem/461/A 题目意思:给出一群由 n 个数组成的集合你,依次循环执行两种操作: (1)每次Toastman得 ...
随机推荐
- ChsLLVMDocs
https://github.com/wuye9036/ChsLLVMDocs/blob/master/CodeGen.md
- go 依赖包管理工具gb安装报错
尝试了下gb工具,发现有个问题: [root@etcd1 test]# go get github.com/constabulary/gb/... /home/gopath/src/github.co ...
- DevExpress右键菜单使用 z
添加BarManager和popupMenu组建: 设置popupMenu组件的Manager属性: 右键点击popupMenu主键,点击Customize选项添加菜单: 然后便可添加到鼠标事件 ...
- Mac下export生效
在Terminal下用export PS1=XXX 修改完后,本次生效,但是重新启动Teminal后又恢复到默认格式.如何才能永久保存自定义的提示符格式呢? 1,~下面本来没有 .bash_pro ...
- 转:如何mac下使用wireshark
Mac OS Mountain Lion默认是没有安装X11的,而wireshark运行需要x11,因此如果直接安装wireshark而没有安装x11,wireshark不会正常运行. 去苹果主页下载 ...
- navicat小经验和快捷键
1.有时按快捷键Ctrl+F搜某条数据的时候搜不到,但是能用sql查出来,这是怎么回事? Ctrl+F只能搜本页数据,不在本页的数据搜不到,navicat每页只显示1000条数据.在数据多的时候nav ...
- Irrlicht 3D Engine 笔记系列之 教程4 - Movement
作者: i_dovelemon 日期: 2014 / 12 / 16 来源: CSDN 主题: Event Receiver, Animator, Framerate independent move ...
- selenium之 文件上传方法
文件上传是所有UI自动化测试都要面对的一个头疼问题 首先,我们要区分出上传按钮的种类,大体上可以分为两种,一种是input框,另外一种就比较复杂,通过js.flash等实现,标签非input 我们分别 ...
- hdu 5316 Magician 线段树
链接:http://acm.hdu.edu.cn/showproblem.php? pid=5316 Magician Time Limit: 18000/9000 MS (Java/Others) ...
- laravel 将数组转化成字符串 再把字符串转化成数组
这是在给阮少翔改代码的时候用的方法, 开始的数据用explored转化成数组不是想要的结果, 我就自己写了一个方法把有用的信息提取出来拼接成一个字符串, 再用explored将字符串转化成数组. ...