1.check(int i)函数 检查当前长度i 在 N 块巧克力能分多少块相同大小的正方体 值为sum
如果sum>=要求分的块数返回true 否则返回false
2.图示
import java.util.Scanner;
public class Main {
static int number;
static int end = Integer.MIN_VALUE;
static int [][]arr;
static int N;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
N = in.nextInt();
number = in.nextInt();
arr = new int[N][2];
for(int i=0;i<N;i++){
arr[i][0] = in.nextInt();
arr[i][1] = in.nextInt();
}
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[0].length;j++){
if(end<arr[i][j]) end = arr[i][j];
}
}
binarySearch();
}
static void binarySearch() {
int l = 1, r = end;
while(l < r){
int mid = l+r+1 >>1;
if(check(mid)) l = mid;
else r = mid-1;
}
System.out.println(l);
}
static boolean check(int mid) {
int sum = 0;
for(int i=0;i<arr.length;i++){
int n = (int) Math.floor(arr[i][0]/mid);
int m = (int) Math.floor(arr[i][1]/mid);
sum = sum + n*m;
}
if(sum>=number) return true;
else return false;
}
}