-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC_Cellular_Network.cpp
More file actions
44 lines (43 loc) · 1.51 KB
/
C_Cellular_Network.cpp
File metadata and controls
44 lines (43 loc) · 1.51 KB
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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const char nl = '\n';
// Author oGhostyyy
/* Thinking: n points in a line , and m more points , provided upto r distnace
iterate through the cities find their closest tower max is the answer? */
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n; cin >> n; //number of cities
int m; cin >> m; //number of towers
vector<int> a(n); for(auto& it: a) cin >> it;
vector<int> b(m); for(auto& it : b) cin >> it;
//towers in a set to keep em sorted and dups gone
set<int> towers;
for(int i = 0; i < m; i++){
towers.insert(b[i]);
}
int ans = 0;
for(int i = 0; i < n; i++){//iterate over cities
int cur = a[i];
int dist = INT_MAX;
auto range = towers.equal_range(cur);//find upper and lower in that set
auto lower = range.first;
auto upper = range.second;//got the upper and lower bounds
if(lower != upper){//when they're not equal you found the exact value [,(
dist = 0;
} else {
//nearest on da right
if(lower != towers.end()){
dist = *lower - cur;
}
//nearest on da left , reduce by 1 tho
if(lower != towers.begin()){
lower--; //decerement pointer, this one will be lesser than cur
dist = min(dist, cur - *lower); //pick min from left or right
}
}
ans = max(ans, dist);
}
cout << ans << nl;
}