-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHackerLandRadio.cpp
58 lines (48 loc) · 1.09 KB
/
HackerLandRadio.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
// problme: "https://www.hackerrank.com/challenges/hackerland-radio-transmitters/problem"
#include <bits/stdc++.h>
using namespace std;
int hackerlandRadioTransmitters(vector <int>&x, int k, int n)
{
map< int,int >m;
// Complete this function
for(int i=0;i!=n;i++)
m[x[i]]++;
int count=0;
auto base=m.begin();
auto top=m.begin();
while(base!=m.end()&&top!=m.end())
{
while(top->first-base->first<=k&&top!=m.end())
top++;
if(top!=m.end())
{
base=--top;
top++;
base->second=0;
count++;
}
while(top->first-base->first<=k&&top!=m.end())
top++;
if(top!=m.end())
{
base=top;
top++;
}
}
if(base->second==0)
return count;
else
return count+1;
}
int main() {
int n;
int k;
cin >> n >> k;
vector<int> x(n);
for(int x_i = 0; x_i < n; x_i++){
cin >> x[x_i];
}
int result = hackerlandRadioTransmitters(x, k, n);
cout << result << endl;
return 0;
}