来自不同国家/地区的每位球员可能拥有的最大 K 队人数
给定一个由N个正整数和一个正整数K组成的数组arr[] ,使得有N个国家,每个国家有arr[i]个球员,任务是找到可以通过组建的球队组成的最大球队数大小为K ,这样球队中的每个球员都来自不同的国家。
例子:
Input: N = 4, K = 3, arr[] = {4, 3, 5, 3}
Output: 5
Explanation:
Consider the countries are named A, B, C and D. The possible ways of forming the teams are {A, B, C}, {A, C, D}, {A, B, C}, {B, C, D}, {A, C, D} such that in each set there are no more than 1 person from a country.
Therefore, the total count teams formed is 5.
Input: N = 3, K = 2, arr[] = {2, 3, 4}
Output: 4
Explanation:
Consider the countries are named A, B, C and D. The possible ways of forming the teams are {B, C}, {B, C}, {A, C}, ({A, B} or {A, C} or {B, C}) such that in each set there are no more than 1 person from a country.
Therefore, the total count teams formed is 4.
方法:给定的问题可以通过使用二分搜索来解决,其思想是对可以形成的团队数量进行二分搜索。让这个变量是T 。对于T的每个值,检查是否可以从每个国家/地区的给定球员列表中组建T队。如果[0, N – 1]范围内所有i的arr[i]或T的最小值之和大于等于T*K ,则可以形成T个团队。请按照以下步骤解决此问题:
- 定义一个函数,比如isPossible(arr, mid, K)来检查是否可以形成中等数量的团队。
- 将变量sum初始化为0以存储数组元素的总和。
- 遍历范围[0, N]并执行以下任务:
- 将mid或arr[i]的最小值添加到变量sum。
- 如果总和大于等于mid*K ,则返回true 。否则,返回false 。
- 初始化变量,比如lb和ub为0和1e9作为可以形成的团队数量的下限和上限。
- 迭代 while 循环直到lb小于等于ub并执行以下步骤:
- 初始化变量,比如mid作为ub和lb的平均值。
- 调用函数isPossible(arr, mid, K)如果函数返回true ,则检查范围的上半部分。否则,检查范围的下半部分。
- 执行上述步骤后,打印mid的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find if T number of teams
// can be formed or not
bool is_possible(vector& teams,
int T, int k)
{
// Store the sum of array elements
int sum = 0;
// Traverse the array teams[]
for (int i = 0; i < teams.size(); i++) {
sum += min(T, teams[i]);
}
// Required Condition
return (sum >= (T * k));
}
// Function to find the maximum number
// of teams possible
int countOfTeams(vector& teams_list,
int N, int K)
{
// Lower and Upper part of the range
int lb = 0, ub = 1e9;
// Perform the Binary Search
while (lb <= ub) {
// Find the value of mid
int mid = lb + (ub - lb) / 2;
// Perform the Binary Search
if (is_possible(teams_list, mid, K)) {
if (!is_possible(
teams_list, mid + 1, K)) {
return mid;
}
// Otherwise, update the
// search range
else {
lb = mid + 1;
}
}
// Otherwise, update the
// search range
else {
ub = mid - 1;
}
}
return 0;
}
// Driver Code
int main()
{
vector arr = { 2, 3, 4 };
int K = 2;
int N = arr.size();
cout << countOfTeams(arr, N, K);
return 0;
}
Java
// Java program for the above approach
class GFG {
// Function to find if T number of teams
// can be formed or not
public static boolean is_possible(int[] teams, int T, int k)
{
// Store the sum of array elements
int sum = 0;
// Traverse the array teams[]
for (int i = 0; i < teams.length; i++) {
sum += Math.min(T, teams[i]);
}
// Required Condition
return (sum >= (T * k));
}
// Function to find the maximum number
// of teams possible
public static int countOfTeams(int[] teams_list, int N, int K)
{
// Lower and Upper part of the range
int lb = 0;
double ub = 1e9;
// Perform the Binary Search
while (lb <= ub) {
// Find the value of mid
int mid = lb + (int) (ub - lb) / 2;
// Perform the Binary Search
if (is_possible(teams_list, mid, K)) {
if (!is_possible(teams_list, mid + 1, K)) {
return mid;
}
// Otherwise, update the
// search range
else {
lb = mid + 1;
}
}
// Otherwise, update the
// search range
else {
ub = mid - 1;
}
}
return 0;
}
// Driver Code
public static void main(String args[]) {
int[] arr = { 2, 3, 4 };
int K = 2;
int N = arr.length;
System.out.println(countOfTeams(arr, N, K));
}
}
// This code is contributed by _saurabh_jaiswal.
Python3
# Python 3 program for the above approach
# Function to find if T number of teams
# can be formed or not
def is_possible(teams, T, k):
# Store the sum of array elements
sum = 0
# Traverse the array teams[]
for i in range(len(teams)):
sum += min(T, teams[i])
# Required Condition
return (sum >= (T * k))
# Function to find the maximum number
# of teams possible
def countOfTeams(teams_list, N, K):
# Lower and Upper part of the range
lb = 0
ub = 1000000000
# Perform the Binary Search
while (lb <= ub):
# Find the value of mid
mid = lb + (ub - lb) // 2
# Perform the Binary Search
if (is_possible(teams_list, mid, K)):
if (is_possible(teams_list, mid + 1, K)==False):
return mid
# Otherwise, update the
# search range
else:
lb = mid + 1
# Otherwise, update the
# search range
else:
ub = mid - 1
return 0
# Driver Code
if __name__ == '__main__':
arr = [2, 3, 4]
K = 2
N = len(arr)
print(countOfTeams(arr, N, K))
# This code is contributed by ipg2016107.
C#
// C# program for the above approach
using System;
class GFG{
// Function to find if T number of teams
// can be formed or not
public static bool is_possible(int[] teams,
int T, int k)
{
// Store the sum of array elements
int sum = 0;
// Traverse the array teams[]
for(int i = 0; i < teams.Length; i++)
{
sum += Math.Min(T, teams[i]);
}
// Required Condition
return (sum >= (T * k));
}
// Function to find the maximum number
// of teams possible
public static int countOfTeams(int[] teams_list,
int N, int K)
{
// Lower and Upper part of the range
int lb = 0;
double ub = 1e9;
// Perform the Binary Search
while (lb <= ub)
{
// Find the value of mid
int mid = lb + (int) (ub - lb) / 2;
// Perform the Binary Search
if (is_possible(teams_list, mid, K))
{
if (!is_possible(teams_list, mid + 1, K))
{
return mid;
}
// Otherwise, update the
// search range
else
{
lb = mid + 1;
}
}
// Otherwise, update the
// search range
else
{
ub = mid - 1;
}
}
return 0;
}
// Driver Code
public static void Main(String[] args)
{
int[] arr = { 2, 3, 4 };
int K = 2;
int N = arr.Length;
Console.WriteLine(countOfTeams(arr, N, K));
}
}
// This code is contributed by code_hunt
Javascript
4
时间复杂度: O(N*log N)
辅助空间: O(1)