AcWing 730. 机器人跳跃问题
原题链接
中等
作者:
uchar
,
2024-12-08 17:04:10
,
所有人可见
,
阅读 18
难点在于防止数据溢出
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
int a[N], maxn, n;
bool check(int x)
{
for (int i = 0; i < n; i ++ )
{
x = x * 2 - a[i];
if (x >= maxn) return true; // 只要大于数组中的最大元素,就会一直单增
if (x < 0) return false;
}
return true;
}
int main()
{
scanf("%d", &n);
int mid;
for (int i = 0; i < n; i ++ )
{
scanf("%d", &a[i]);
if (a[i] > maxn) maxn = a[i];
}
int l = 1, r = 1e5;
while (l < r)
{
mid = l + r >> 1;
if (check(mid)) r = mid;
else l = mid + 1;
}
printf("%d", l);
return 0;
}