java分巧克力
简单二分
Java样例
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int[] h = new int[n];
int[] w = new int[n];
for(int i=0;i<n;i++) {
h[i] = sc.nextInt();
w[i] = sc.nextInt();
}
int l=1,r=(int) 1e5;
while(l<r) {
int mid = (l+r+1)/2;
if(check(mid,h,w)>=k) l=mid;
else r=mid-1;
}
System.out.println(r);
}
public static int check(int mid,int[] h,int[] w) {
int res = 0;
for(int i=0;i<h.length;i++) {
res += (h[i]/mid)*(w[i]/mid);
}
return res;
}
}