### obocheton's blog

By obocheton, history, 8 months ago,

I was trying this problem CSES 1084.

I greedily choose the lowest sized valid apartment for every applicant (sorted both lists ascendingly).

But it turns out to be a wrong approach.

Why my approach is wrong?

• +2

 » 8 months ago, # |   +1 Inserting the apartment sizes into the set may remove some apartments with the same size. Try using a multiset or a regular vector
•  » » 8 months ago, # ^ |   0 what about erasing an element from vector, isn't it O(n)?
 » 8 months ago, # | ← Rev. 3 →   +1 There can be multiple apartments with same size so instead of using set use multiset. And use inbuilt lowerbound function of set as thats faster .And to remove only one element from the multiset use s.erase(it) instead of s.erase(*it) as the later will remove all occurrences of *it. SpoilerYour code here... #include using namespace std; int a[200005]; int main(){ int n,m,k,ans=0; cin>>n>>m>>k; for(int i=0;i>a[i]; } sort(a,a+n); int aa; multisets; for(int i=0;i>aa; s.insert(aa); } for(int i=0;i
•  » » 7 months ago, # ^ |   0 Thanks. I was searching for this one. Your code was helpful for me.
•  » » 7 weeks ago, # ^ |   0 would you describe a bit, that lower bound part? I'm having some problem there.
 » 7 weeks ago, # |   0 Here was my idea previously.Maintain two pointers, left and right. Sort both lists, lets call them A and B.Then the answer is just the number of points of the arrays that match up together (by x).Many people are looking at multi set, but to be honest, I think two pointers is a really easy to understand approach, and it's just as efficient / effective.
 » 7 weeks ago, # |   0 FYI, lower bound on sets is O(n), so you should probably use -> (set name).lower_bound(iterator).
»
6 weeks ago, # |
-9

given problem is not clear that why its take lot of tym to solve. but now i have solution for this.

# include <bits/stdc++.h>

using namespace std;

# define int long long

signed main(){ int n , m , k ; cin >> n >> m >> k; int a[n] ; for(int i = 0 ; i < n ; i++) cin >> a[i]; int b[m] ; for(int i = 0 ; i < m ; i++) cin >> b[i];

sort(a , a+n);
sort(b , b+m);

int i = n - 1 , j = m - 1;

int applicants = 0;

while(i >= 0 && j >= 0){

if (b[j] < a[i] - k)
i--;
else if (b[j] > a[i] + k)
j--;
else
{
applicants++;
i--;
j--;
}
}
cout << applicants << endl;

}