AcWing 1227.
Analysis of the idea of dividing chocolate (portal) :
Simple integer dichotomy. Here we need to focus on the change of the interval, that is, whether mid is rounded up or down. Both methods are ok, but the code is different in the
AC code. Round up
Note: The change of the interval is still affected by the return value of the check function
AC code:
#include <iostream>
#define ll long long
using namespace std;
const int N = 1e5 + 10;
int h[N], w[N];
int n, k;
bool check(int mid) {
ll cnt = 0;
for(int i = 0;i < n ;i ++)
cnt += (h[i] / mid) * (w[i] / mid);
return cnt >= k;
}
int main() {
cin >> n >> k;
for (int i = 0; i < n; i++)
cin >> h[i] >> w[i];
int l = 1,r = 1e5;
int mid;
// 二分模板,这里是向上取整
while(l < r){
mid = l + r + 1 >> 1; // 位运算更好 等价于 mid = (l + r + 1) / 2;
if(check(mid))
l = mid;
else
r = mid - 1;
}
// 出 while 循环时,l == r
cout << l << endl;
return 0;
}