---
# System prepended metadata

title: 3B 公寓出租
tags: [solution]

---

---
title: 3B 公寓出租
tags: solution
---

# B. 公寓出租

- 本題源自於CSES：[Apartments](https://cses.fi/problemset/task/1084)
```cpp=
# include <iostream>
# include <algorithm>
using namespace std;

int a[200000], b[200000];

int main(){
    int N,M,k,n,m,ans = 0;
    cin>>N>>M>>k;

    for(n = 0 ; n<N ; n++)
        cin>>a[n];
    for(n = 0 ; n<M ; n++)
        cin>>b[n];

    sort(a,a+N);
    sort(b,b+M);

    n = m = 0;
    while(n<N && m<M){
        if(abs(a[n]-b[m])<=k)
            ans++, m++, n++;
        else if(b[m] < a[n])
            m++;
        else
            n++;
    }

    cout<<ans;
}
```