AcWing 730. Robot jumping problem

AcWing 730. Robot jumping problem

The robot is playing an old DOS-based game.

There are N+1 buildings in the game-numbered from 0 to N, arranged from left to right.

The height of the building numbered 0 is 0 units, and the height of the building numbered i is H(i) units.

At first, the robot was in the building numbered 0.

At each step, it jumps to the next (right) building.

Assuming that the robot is in the kth building, and its current energy value is E, it will jump to the k+1th building in the next step.

If H(k+1)>E, then the robot will lose the energy value of H(k+1)-E, otherwise it will get the energy value of EH(k+1).

The goal of the game is to reach the Nth building. In this process, the energy value cannot be a negative number of units.

The question now is how much energy should the robot start the game at least to guarantee the successful completion of the game?

Input format
Enter the integer N in the first line.

The second line is an integer separated by N spaces, H(1), H(2),...,H(N) represents the height of the building.

Output format
Output an integer, which represents the result of rounding up the initial energy value of the minimum required unit.

Data range
1≤N, H(i)≤105,

Input example 1:

5
3 4 3 2 4

Output sample 1:

4

Input example 2:

3
4 4 4

Output sample 2:

4

Input sample 3:

3
1 6 4

Output sample 3:

3

This question is relatively simple. I observed that this seems to be an increasing sequence. The dichotomy is solved directly. I wrote the first version.

#include<iostream>

using namespace std;

const int N=1e5+10,Mod=1e9+7;

int h[N];
int n;

bool check(int m)
{
    
    
    long long mid=m;
    for(int i=1;i<=n;i++)
    {
    
    
        if(h[i]>mid)
        mid-=h[i]-mid;
        else
        mid+=mid-h[i];
        mid%=Mod;
        if(mid<0)
        return false;
    }
    return true;
}

int main(void)
{
    
    
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&h[i]);
    int l=0,r=1e5;
    while(l<r)
    {
    
    
        int mid=l+r>>1;
        if(check(mid)) r=mid;
        else l=mid+1;
    }
    cout<<l;
}

Insert picture description here

I haven't tried this test example. I observed it carefully. It turned out to be an int burst, so I improved the check function.

#include<iostream>

using namespace std;

const int N=1e5+10,Mod=1e9+7;

int h[N];
int n;

bool check(int m)
{
    
    
    long long mid=m;
    for(int i=1;i<=n;i++)
    {
    
    
        if(h[i]>mid)
        mid-=h[i]-mid;
        else
        mid+=mid-h[i];
        if(mid>1e5+10)
        return true;
        else if(mid<0)
        return false;
    }
    return true;
}

int main(void)
{
    
    
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&h[i]);
    int l=0,r=1e5;
    while(l<r)
    {
    
    
        int mid=l+r>>1;
        if(check(mid)) r=mid;
        else l=mid+1;
    }
    cout<<l;
}

Successful AC

Guess you like

Origin blog.csdn.net/qq_52358098/article/details/114060250