您的位置:首页 > 大数据 > 人工智能

Codeforces 463B Caisa and Pylons【二分】

2016-12-13 21:11 239 查看
B. Caisa and Pylons

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

Caisa solved the problem with the sugar and now he is on the way back to home.

Caisa is playing a mobile game during his path. There are
(n + 1) pylons numbered from 0 to
n in this game. The pylon with number 0 has zero height, the pylon with number
i (i > 0) has height
hi. The goal of the game is to reach
n-th pylon, and the only move the player can do is to jump from the current pylon (let's denote its number as
k) to the next one (its number will be
k + 1). When the player have made such a move, its energy increases by
hk - hk + 1 (if this value is negative the player loses energy). The player must have non-negative amount of energy at any moment
of the time.

Initially Caisa stand at 0 pylon and has
0 energy. The game provides a special opportunity: one can pay a single dollar and increase the height of anyone pylon by one. Caisa may use that opportunity several times, but he doesn't want to spend too much money. What is the minimal amount of money
he must paid to reach the goal of the game?

Input
The first line contains integer n
(1 ≤ n ≤ 105). The next line contains
n integers h1,
h2, ...,
hn
(1  ≤  hi  ≤  105) representing the heights of the pylons.

Output
Print a single number representing the minimum number of dollars paid by Caisa.

Examples

Input
5
3 4 3 2 4


Output
4


Input
3
4 4 4


Output
4


Note
In the first sample he can pay 4 dollars and increase the height of pylon with number
0 by 4 units. Then he can safely pass to the last pylon.

题目大意:

一共有N个地方要走,我们处于位子0,其a【0】=0,我们如果从位子i走到位子j,那么相对得到a【i】-a【j】点能量,如果为负,则为减少,我们需要保证从位子0走到位子N,整个过程不能有能量为负数的时候发生,但是我们可以花费1单位换来某位子的a【i】++,问最少花费,使得从位子0能够到达最后的位子。

思路:

对应我们可以将所有问题转化到起点位子上来,只要我们在最初从位子0到达位子1之后,其获得的能量足够后边的路程使用即可,那么我们考虑对位子0的花费进行枚举.但是很显然,直接枚举是一个很大的操作量,但是考虑到:

花费越多,越能够走到终点,那么这里存在单调性,我们可以考虑使用二分查找的思路来优化这个问题。

时间复杂度:O(nlogn);

Ac代码:

#include<stdio.h>
#include<string.h>
using namespace std;
#define ll __int64ll a[150000];
int n;
int Slove(ll mid)
{
a[0]=mid;
ll sum=0;
for(int i=1;i<=n;i++)
{
sum+=a[i-1]-a[i];
if(sum<0)return 0;
}
return 1;
}
int main()
{
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
{
scanf("%I64d",&a[i]);
}
ll ans=-1;
ll l=a[1];
ll r=1000000000000;
while(r-l>=0)
{
ll mid=(l+r)/2;
if(Slove(mid)==1)
{
r=mid-1;
ans=mid;
}
else
{
l=mid+1;
}
}
printf("%I64d\n",ans);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Codeforces 463B