Two points of robot jumping problem
The minimum is 0, and the maximum is the maximum value of the H[] array, and then dichotomy
! ! ! ! Note that in the get array, if the accumulated energy is greater than the maximum value, 1 is returned, otherwise the energy will accumulate to overflow, resulting in an error in the result
#include<iostream>
using namespace std;
int h[100005];
int n;
int maxx=0;
bool get(int xx)
{
long long ene=xx;
for(int i=1;i<=n;i++)
{
ene=(ene+ene-h[i]);
// cout<<ene<<" "<<i<<endl;
if(ene<0)
{
// cout<<endl<<"***"<<i<<"****";
return 0;
}
if(ene>maxx) //!!!!!!!!
return 1;
}
return 1;
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
scanf("%d",&h[i]);
maxx=max(maxx,h[i]);
}
int l=0;
int r=maxx+1;
int mid;
// cout<<get(23);
while(l<r)
{
mid=(l+r)/2;
if(get(mid))
{
r=mid;
}else {
l=mid+1;
}
}
cout<<l;
return 0;
}