(l,r)区间二分查找,左端点l,平均值,右端点r最大值
计算当前速度所需时间的函数eatTime()
import java.util.Scanner; public class Main { public static int eatTime(int[] ints, int v){ int time = 0; for(int anInt : ints){ if(anInt % v == 0){ time += anInt / v; }else{ time += anInt / v + 1; } } return time; } public static void main(String[] args){ Scanner scanner = new Scanner(System.in); String[] s = scanner.nextLine().split(" "); int h = scanner.nextInt(); int[] ints = new int[s.length]; int l = 0, r = 0; for(int i = 0; i < ints.length; i++){ ints[i] = Integer.parseInt(s[i]); l += ints[i]; r = Math.max(r, ints[i]); } l /= h; while (r - l > 1) { int mid = (l + r) / 2; if(eatTime(ints, mid) <= h){ r = mid; }else if(eatTime(ints, mid) > h){ l = mid; } } System.out.println(r); } }