二分,用check函数检查当前边长合不合适
#include<iostream> #include<cstring> #include<algorithm> using namespace std ; typedef long long LL ; const int N = 1e5+10 ; int n , k ; int h[N],w[N] ; bool check(int x){ int cnt = 0; for(int i = 0 ; i < n ; i ++){ cnt +=(h[i]/x) * (w[i]/x) ;//麻痹这里记得加括号,我真服了 } if(cnt >= k) return true; else return false ; } int main(){ cin >> n >> k ; for(int i = 0 ; i < n ; i ++) cin >> h[i] >> w[i] ; int l = 1 , r = N ; while(l < r ){ int mid = (l + r + 1) >> 1 ; if(check(mid))l = mid ; else r = mid - 1 ; } cout << l<< endl ; return 0 ; }