码迷,mamicode.com
首页 > 其他好文 > 详细

730. 机器人跳跃问题

时间:2020-02-06 14:46:21      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:puts   mes   stream   while   https   tps   就是   def   min   

题目链接:

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;
}

 

730. 机器人跳跃问题

标签:puts   mes   stream   while   https   tps   就是   def   min   

原文地址:https://www.cnblogs.com/doubest/p/12268485.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!