AcWing 730. 机器人跳跃问题

🤠 原题地址
在这里插入图片描述
在这里插入图片描述

🤠 二分:单调性,二段性,
check函数:
① 符合 递归左区间 r = mid,mid可能会是答案;
② 不符合,递归右区间 l = mid +1 mid 不可能是答案、
🤠 二段性:当 mid 符合条件时,大于 mid 的都符合条件,但小于mid 的就不一定符合条件了

 

import java.io.*;
import java.util.*;

public class Main
{
	static int n;
	static int N = 100010;
	static int[] h = new int[N];
	static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));

	public static void main(String[] args) throws NumberFormatException, IOException
	{
		n = Integer.parseInt(in.readLine());
		String[] a = in.readLine().split(" ");
		for (int i = 0; i < n; i++)
		{
			h[i] = Integer.parseInt(a[i]);
		}
		int l = 0;
		int r = (int) 1e5;
		while (l < r)
		{
			int mid = l + r >> 1;
			if (check(mid))
			{
				r = mid;
			} else
			{
				l = mid + 1;
			}
		}
		System.out.println(l);
	}

	private static boolean check(int e)
	{
		for (int i = 0; i < n; i++)
		{
			e = e * 2 - h[i];
			if (e > (int) 1e5)
			    return true;
		    if (e < 0)
			    return false;
		}
		
		return true;
	}
}

posted @ 2023-03-05 14:20  兑生  阅读(27)  评论(0)    收藏  举报  来源
Live2D