Loading

730. 机器人跳跃问题

题目链接:

https://www.acwing.com/problem/content/732/

题解:

这个题最重要的一点就是:用初始值丈量能不能行时,一定要及时停止,不然连long long都给你爆了。

二分的两个边界:

l = (min + 1) / 2

r = max

原因是:

x + x - min >= 0

x-max > 0 没意义 所以 x <= max

 

AC代码:

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

using namespace std;

typedef long long ll;
int const N = 1e5+5;
ll nums[N];
int n;
ll maxll = -1;
ll minll = 1e40;

ll check(int mid){
    ll res = mid;
    for(int i=0;i<n;i++){
        res = res * 2 - nums[i];
        // printf("%lld ",res);
        if(res >= maxll){
            return maxll;
        }
        if(res < 0){
            return -1;
        }
    }
    // puts("");
    return res;
}

int main(void){
    scanf("%d",&n);
    for(int i=0;i<n;i++){ 
        scanf("%lld",&nums[i]);
        if(nums[i] > maxll){
            maxll = nums[i];
        }
        if(nums[i] < minll){
            minll = nums[i];
        }
    }
    int res;
    int l = (minll+1)/2;
    int r = maxll;
    int mid;
    while(l < r){
        mid = l+r >> 1;
        ll tmp = check(mid);
        // printf("mid: %d tmp : %lld\n" ,mid,tmp);
        if(tmp >= 0) r = mid;
        else l = mid+1;
    }
    
    printf("%d",l);
    return 0;
}

 

posted @ 2020-02-06 13:55  Doubest  阅读(213)  评论(0编辑  收藏  举报