给定两个数组A [] (由N个整数组成)和B [] (由M个巧克力的口味值和一个整数X组成) ,任务是根据以下条件找到可以接收巧克力的最大人数:一个人只能吃一种巧克力,其口味值在[A [i] – X,A [i] + X]范围内。
注意:一旦将巧克力送给任何人,就不能将其送给任何其他人。
例子:
Input: A[] = {90, 49, 20, 39, 60}, B[] = {14, 24, 82}, X = 15
Output: 3
Explanation:
1st person can pick the 3rd chocolate as the value of the 3rd chocolate ( = 82 ) lies in the range [75 ( = 90 – 15 ), 105 ( = 90 + 15)].
2nd person can’t pick any chocolate because there is no chocolate with value in the range [34 ( = 49 – 15 ), 64 ( = 49 + 15).
3rd person can pick the 1st chocolate as the value of the 1st chocolate lies in the range [5 ( = 20 – 15), 35 ( = 20 – 15)].
4th person can pick the 2nd chocolate because value of the 2nd chocolate lies in the range [ 24 ( = 39 – 15) and 54 ( = 39 – 15)].
5th person can’t pick any chocolate because there is no chocolate with value in the range [45 ( = 60 – 15), 75 ( = 60 – 15)].
Therefore, the total number of people receiving a chocolate is 3, which is the maximum possible.
Input: A[] = {2, 4, 6, 40, 50}, B[] = {38, 36}, X=13
Output: 2
方法:可以使用贪婪技术/ a>和搜索来解决此问题。这里的关键观察为任何第i个人,分配与最小的可能值处于范围,如果可能的话巧克力。否则,从结果中排除该人。请按照以下步骤操作:
- 以非降序对给定数组A []和B []进行排序。
- 初始化一个多集以存储数组B []的元素。
- 初始化变量count = 0 ,以存储接受巧克力的人数。
- 遍历数组A []并使用Binary Search或lower_bound()查找可以分配给每个第i个人的最小巧克力值。检查该值是否在[a i – X,a i + X]范围内。
- 如果发现是真,则增加计数并从多组中除去该巧克力的值。
- 完成上述步骤后,打印count的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count the maximum number
// of persons receiving a chocolate
int countMaxPersons(int* A, int n, int* B,
int m, int x)
{
// Initialize count as 0
int count = 0;
// Sort the given arrays
sort(A, A + n);
sort(B, B + m);
// Initialize a multiset
multiset s;
// Insert B[] array values into
// the multiset
for (int i = 0; i < m; i++)
s.insert(B[i]);
// Traverse elements in array A[]
for (int i = 0; i < n; i++) {
int val = A[i] - x;
// Search for the lowest value in B[]
auto it = s.lower_bound(val);
// If found, increment count,
// and delete from set
if (it != s.end()
&& *it <= A[i] + x) {
count++;
s.erase(it);
}
}
// Return the number of people
return count;
}
// Driver Code
int main()
{
int A[] = { 90, 49, 20, 39, 49 };
int B[] = { 14, 24, 82 };
int X = 15;
int N = sizeof(A) / sizeof(A[0]);
int M = sizeof(B) / sizeof(B[0]);
// Function Call
cout << countMaxPersons(A, N, B, M, X);
}
Python3
# Python3 program for the above approach
from bisect import bisect_left
# Function to count the maximum number
# of persons receiving a chocolate
def countMaxPersons(A, n, B, m, x):
# Initialize count as 0
count = 0
# Sort the given arrays
A = sorted(A)
B = sorted(B)
# Initialize a multiset
s = []
# Insert B[] array values into
# the multiset
for i in range(m):
s.append(B[i])
# Traverse elements in array A[]
for i in range(n):
val = A[i] - x
# Search for the lowest value in B[]
it = bisect_left(s,val)
# If found, increment count,
# and delete from set
if (it != len(s) and it <= A[i] + x):
count += 1
del s[it]
# Return the number of people
return count
# Driver Code
if __name__ == '__main__':
A = [90, 49, 20, 39, 49]
B = [14, 24, 82]
X = 15
N = len(A)
M = len(B)
# Function Call
print(countMaxPersons(A, N, B, M, X))
# This code is contributed by mohit kumar 29
3
时间复杂度: O(N * log N)
辅助空间: O(M)