while(l + 1 < r) {
int mid = l + r >> 1;
if(check(mid)) l = mid;
else r = mid;
}
class Solution { public: int findRadius(vector<int>& houses, vector<int>& heaters) { int n = houses.size(), m = heaters.size(); sort(houses.begin(), houses.end()); sort(heaters.begin(), heaters.end()); auto check = [&](int rid) -> bool { for(int i = 0, j = 0; i < n; i ++ ) { while(j < m && houses[i] > heaters[j] + rid) j ++ ; if(j < m && heaters[j] - rid <= houses[i] && heaters[j] + rid >= houses[i]) continue; return false; } return true; }; int l = -1, r = 1e9; while(l + 1 < r) { int mid = l + r >> 1; if(check(mid)) r = mid; else l = mid; } return r; } };
标签:二分,heaters,int,mid,houses,算法,rid,check From: https://www.cnblogs.com/zk6696/p/17789138.html