CSES - Shared codeLink to this code:
https://cses.fi/paste/7b480213598453e365ed1b/
#pragma GCC optimize("O2")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#undef NDEBUG
#include <bits/stdc++.h>
#define nl << "\n"
typedef long long ll;
#define vi vector<ll>
#define vp vector<pair<ll, ll>>
#define ump unordered_map<ll, ll>
#define ucmp unordered_map<char, ll>
#define usmp unordered_map<string, ll>
#define ust unordered_set<ll>
#define mp map<ll, ll>
#define mpss map<string, string>
#define st set<ll>
#define mmp multimap<ll, ll>
#define mst multiset<ll>
#define ummp unordered_multimap<ll, ll>
#define umst unordered_multiset<ll>
#define fr(i, a, b) for (ll i = a; i < b; i++)
#define frr(i, a, b) for (ll i = a; i >= b; i--)
#define pb push_back
#define pf push_front
#define all(a) a.begin(), a.end()
#define mne(a) min_element(all(a))
#define mxe(a) max_element(all(a))
#define MAX INT_MAX
#define vs vector<string>
#define sz(a) a.size() * 1ll
#define pi pair<int, int>
#define F first
#define S second
#define vvi vector<vi>
#define frsq(i, a, b) for (ll i = a; (i * i) < b; i++)
#define frp(a, b) for (auto &a : b)
#define sp(a) fixed << setprecision(a)
#define sq(e) e *e
#define inp(a) frp(x, a) cin >> x;
const ll mod = 1e16;
const ll maxu = 100005;
using namespace std;
ll mu(ll a, ll b)
{
return (((a % mod) * (b % mod)) % mod);
}
ll bp(ll a, ll b)
{
ll res = 1;
while (b)
{
if (b & 1)
{
res = (res * a); //% mod;
}
a = (a * a); //% mod;
b >>= 1;
}
return res;
}
void solve()
{
ll n,m,k; cin>>n>>m>>k;
vi c(n); inp(c);
mst s;
fr(i,0,m){
ll e; cin>>e;
s.insert(e);
}
sort(all(c));
fr(i,0,n){
if(sz(s)==0) break;
auto it=lower_bound(all(s),c[i]);
if(it==s.end()){
--it;
if(abs(*it-c[i])<=k)
s.erase(it);
continue;
}
ll x=*it;
if(it!=s.begin()) it--;
else{
if(abs(x-c[i])<=k)
s.erase(it);
continue;
}
ll y=*it;
if(abs(y-c[i])<=k){
s.erase(it);
}else if(abs(x-c[i])<=k){
it++;
s.erase(it);
}
}
cout<<m-sz(s);
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
ll t = 1;
//cin >> t;
while (t--)
{
solve();
cout nl;
}
return 0;
}