AcWing 730. Robot jumping problem (two points)

The question
must be read carefully to understand the meaning of
the question. This question is to increase or decrease the energy based on the energy of the previous point e; formula: 2*eH(i)

Use dichotomous thinking to find the answer, the last l or r is the answer

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 100010;

int n;
int h[N];

bool check(int e)
{
    
    
    for (int i = 1; i <= n; i ++ )
    {
    
    
        e = e * 2 - h[i];
        if (e >= 1e5) return true;//此处不能省略,因为如果一直乘2,则会出现溢出现象,e可能超级大变为负值 
        if (e < 0) return false;
    }
    return true;
}

int main()
{
    
    
    scanf("%d", &n);
    for (int i = 1; i <= n; i ++ ) scanf("%d", &h[i]);

    int l = 0, r = 1e5;//设置高度的最小值和最大值 
    while (l < r)//对应二分的模板一 (AcWing 789. 数的范围) 
    {
    
    
        int mid = l + r >> 1;
        if (check(mid)) r = mid;
        else l = mid + 1;
    }

    printf("%d\n", l);//二分结束时  l=r

    return 0;
}

Guess you like

Origin blog.csdn.net/qq_47874905/article/details/114945960