-
Notifications
You must be signed in to change notification settings - Fork 0
/
2. Apartments.cpp
70 lines (64 loc) · 1.69 KB
/
2. Apartments.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
#include "iostream"
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef set<ll> sl;
typedef map<char, ll> mcl;
typedef map<ll, ll> mll;
typedef pair<char, ll> pcl;
#define PB push_back
#define F first
#define S second
#define REP(i,a,b) for (ll i = a; i < b; i++)
#define REPR(i,a,b) for (ll i = a; i > b; i--)
#define REPJ(i,a,b,j) for (ll i = a; i < b; i+=j)
#define REPIT(it,o) for (auto it = o.begin(); it != o.end(); it++)
#define REPITR(it,o) for (auto it = o.rbegin(); it != o.rend(); it++)
int main() {
ll n,m,k;
cin >> n >> m >> k;
vl tenants;
vl apartments;
REP(i,0,n){
ll tmp;
cin >> tmp;
tenants.PB(tmp);
}
REP(i,0,m){
ll tmp;
cin >> tmp;
apartments.PB(tmp);
}
ll ans = 0;
sort(apartments.begin(), apartments.end());
sort(tenants.begin(), tenants.end());
ll t = 0;
ll a = 0;
while(a < apartments.size()){
if (t >= tenants.size()){
break;
}
if (apartments[a] >= tenants[t] - k && apartments[a] <= tenants[t] + k){
t++;
a++;
ans++;
} else if (tenants[t] < apartments[a]){
t++;
} else {
a++;
}
}
cout << ans;
}
// - first approach
// sort both lists. nested loop to find the soln
// tle
// - second approach
// sort both. loop over apartments and search for tenants using a binary search algo(logn). if found remove that tenant from the list
// tle
// - third approach
// sort both. loop over simultaneously, check for eligibility and skip over whichevers value is less
// ac