-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path475.cpp
71 lines (66 loc) · 2.17 KB
/
475.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
__________________________________________________________________________________________________
sample 36 ms submission
class Solution {
public:
int findRadius(vector<int>& houses, vector<int>& heaters) {
int length = heaters.size();
sort(heaters.begin(), heaters.end());
int max = 0;
for (int& h : houses) {
// cout << "h: " << h << endl;
auto hb = upper_bound(heaters.begin(), heaters.end(), h);
int n = distance(heaters.begin(), hb);
int diff = 0;
if (n == length) diff = (h - heaters[length-1]);
else if (n == 0) diff = (heaters[0] - h);
else diff = (min(heaters[n] - h, h - heaters[n-1]));
if (diff > max) max = diff;
}
return max;
}
};
auto ___ = []() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
return 0;
}();
__________________________________________________________________________________________________
sample 11012 kb submission
class Solution {
public:
int findRadius(vector<int>& houses, vector<int>& heaters) {
sort(heaters.begin(),heaters.end());
int result=0;
for(const auto &h:houses){
int distance=binary_helper(h,heaters);
cout<<distance<<endl;
result=max(result,distance);
}
return result;
}
//在heaters中寻找元素,使得最接近h。
int binary_helper(int h,vector<int>& heaters){
long long l=-1,r=heaters.size();
int right=INT_MAX,left=INT_MAX; //时刻记录
int dis=INT_MAX;
while(l+1!=r){
long long mid=l+(r-l)/2;
int heater=heaters[mid];
if(heater==h) return 0;
else if(heater<h){
l=mid;
right=h-heater;
dis=min(dis,h-heater);
}
else{
r=mid;
dis=min(dis,heater-h);
left=heater-h;
}
}
return dis;
return min(left,right);
}
};
__________________________________________________________________________________________________