There are � n applicants and � m free apartments. Your task is to distribute the apartments so that as many applicants as possible will get an apartment. Each applicant has a desired apartment size, and they will accept any apartment whose size is clos
时间: 2024-03-02 09:50:55 浏览: 31
This problem can be solved using a greedy approach. First, sort the applicants' desired apartment sizes and the available apartment sizes in non-decreasing order. Then, iterate through the applicants and for each applicant, find the smallest available apartment that is either the same size or larger than the applicant's desired size. If such an apartment exists, assign it to the applicant and remove it from the list of available apartments. If not, move on to the next applicant. Repeat this process until all applicants have been assigned an apartment or there are no more available apartments.
Here's some sample code in Python:
```
n, m, k = map(int, input().split())
applicants = sorted(list(map(int, input().split())))
apartments = sorted(list(map(int, input().split())))
i = j = count = 0
while i < n and j < m:
if abs(applicants[i] - apartments[j]) <= k:
count += 1
i += 1
j += 1
elif applicants[i] < apartments[j]:
i += 1
else:
j += 1
print(count)
```
This code reads in the input and sorts the applicants' desired sizes and the available apartment sizes. Then, it iterates through the applicants and available apartments using two pointers, i and j respectively. If an available apartment is found that is close enough in size to an applicant's desired size, the applicant is assigned the apartment and the count is incremented. If not, the pointers are moved forward. Finally, the count of assigned apartments is printed.