有两个进程 P1 和 P2,以及 N 个资源,其中 N 是偶数。有一个 N 大小的数组,arr[i] 表示第 i 个资源的类型。资源的实例可能不止一个。您要在 P1 和 P2 之间平均分配这些资源,以便最大数量的不同资源数量分配给P2。打印分配给 P2 的不同资源的最大数量。
例子:
Input : arr[] = [1, 1, 2, 2, 3, 3]
Output: 3
Explanation:
There are three different kinds of resources (1, 2 and 3), and two for each kind. Optimal distribution: Process P1 has resources [1, 2, 3] and the process P2 has gifts [1, 2, 3], too. Process p2 has 3 distinct resources.
Input: arr[] = [1, 1, 2, 1, 3, 4]
Output: 3
Explanation:
There are three different kinds of resources (1, 2, 3, 4), 3 instances of 1 and single single instances of resource 2, 3, 4. Optimal distribution: Process P1 has resources [1, 1, 1] and the process P2 has gifts [2, 3, 4].
Process p2 has 3 distinct resources.
方法一(使用排序):
- 对资源数组进行排序。
- 通过比较已排序数组的相邻元素找出唯一的元素。假设 count 保存数组中不同数量的资源。
- 返回计数和 N/2 的最小值。
下面是上述方法的实现:
C++
// C++ program to equally divide n elements
// into two sets such that second set has
// maximum distinct elements.
#include
#include
using namespace std;
int distribution(int arr[], int n)
{
sort(arr, arr + n);
int count = 1;
for (int i = 1; i < n; i++)
if (arr[i] > arr[i - 1])
count++;
return min(count, n / 2);
}
// Driver code
int main()
{
int arr[] = { 1, 1, 2, 1, 3, 4 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << distribution(arr, n) << endl;
return 0;
}
Java
// Java program to equally divide n elements
// into two sets such that second set has
// maximum distinct elements.
import java.util.*;
class Geeks {
static int distribution(int arr[], int n)
{
Arrays.sort(arr);
int count = 1;
for (int i = 1; i < n; i++)
if (arr[i] > arr[i - 1])
count++;
return Math.min(count, n / 2);
}
// Driver code
public static void main(String args[])
{
int arr[] = { 1, 1, 2, 1, 3, 4 };
int n = arr.length;
System.out.println(distribution(arr, n));
}
}
// This code is contributed by ankita_saini
Python3
# Python 3 program to equally divide n
# elements into two sets such that second
# set has maximum distinct elements.
def distribution(arr, n):
arr.sort(reverse = False)
count = 1
for i in range(1, n, 1):
if (arr[i] > arr[i - 1]):
count += 1
return min(count, n / 2)
# Driver code
if __name__ == '__main__':
arr = [1, 1, 2, 1, 3, 4]
n = len(arr)
print(int(distribution(arr, n)))
# This code is contributed by
# Shashank_Sharma
C#
// C# program to equally divide
// n elements into two sets such
// that second set has maximum
// distinct elements.
using System;
class GFG
{
static int distribution(int []arr, int n)
{
Array.Sort(arr);
int count = 1;
for (int i = 1; i < n; i++)
if (arr[i] > arr[i - 1])
count++;
return Math.Min(count, n / 2);
}
// Driver code
public static void Main(String []args)
{
int []arr= { 1, 1, 2, 1, 3, 4 };
int n = arr.Length;
Console.WriteLine(distribution(arr, n));
}
}
// This code is contributed
// by ankita_saini
PHP
$arr[$i - 1])
$count++;
return min($count, $n / 2);
}
// Driver code
$arr = array(1, 1, 2, 1, 3, 4 );
$n = count($arr);
echo(distribution($arr, $n));
// This code is contributed
// by inder_verma
?>
Javascript
C++
// C++ program to equally divide n elements
// into two sets such that second set has
// maximum distinct elements.
#include
using namespace std;
int distribution(int arr[], int n)
{
set > resources;
// Insert all the resources in the set
// There will be unique resources in the set
for (int i = 0; i < n; i++)
resources.insert(arr[i]);
// return minimum of distinct resources
// and n/2
int m = resources.size();
return min(m, n / 2);
}
// Driver code
int main()
{
int arr[] = { 1, 1, 2, 1, 3, 4 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << distribution(arr, n) << endl;
return 0;
}
Java
// Java program to equally divide n elements
// into two sets such that second set has
// maximum distinct elements.
import java.util.*;
class GFG
{
static int distribution(int arr[], int n)
{
Set resources = new HashSet();
// Insert all the resources in the set
// There will be unique resources in the set
for (int i = 0; i < n; i++)
resources.add(arr[i]);
// return minimum of distinct resources
// and n/2
return Math.min(resources.size(), n / 2);
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 1, 2, 1, 3, 4 };
int n = arr.length;
System.out.print(distribution(arr, n) +"\n");
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program to equally divide n elements
# into two sets such that second set has
# maximum distinct elements.
def distribution(arr, n):
resources = set()
# Insert all the resources in the set
# There will be unique resources in the set
for i in range(n):
resources.add(arr[i]);
# return minimum of distinct resources
# and n/2
return min(len(resources), n // 2);
# Driver code
if __name__ == '__main__':
arr = [ 1, 1, 2, 1, 3, 4 ];
n = len(arr);
print(distribution(arr, n), "");
# This code is contributed by PrinciRaj1992
C#
// C# program to equally divide n elements
// into two sets such that second set has
// maximum distinct elements.
using System;
using System.Collections.Generic;
class GFG
{
static int distribution(int []arr, int n)
{
HashSet resources = new HashSet();
// Insert all the resources in the set
// There will be unique resources in the set
for (int i = 0; i < n; i++)
resources.Add(arr[i]);
// return minimum of distinct resources
// and n/2
return Math.Min(resources.Count, n / 2);
}
// Driver code
public static void Main(String[] args)
{
int []arr = { 1, 1, 2, 1, 3, 4 };
int n = arr.Length;
Console.Write(distribution(arr, n) +"\n");
}
}
// This code is contributed by PrinciRaj1992
Javascript
输出:
3
时间复杂度– O(N log N)
方法二(使用散列集)
另一种找出不同元素的方法是集合,插入集合中的所有元素。根据集合的属性,它将只包含唯一元素。最后,我们可以计算集合中元素的数量,例如计数。要返回的值将再次由 min(count, n/2) 给出。
C++
// C++ program to equally divide n elements
// into two sets such that second set has
// maximum distinct elements.
#include
using namespace std;
int distribution(int arr[], int n)
{
set > resources;
// Insert all the resources in the set
// There will be unique resources in the set
for (int i = 0; i < n; i++)
resources.insert(arr[i]);
// return minimum of distinct resources
// and n/2
int m = resources.size();
return min(m, n / 2);
}
// Driver code
int main()
{
int arr[] = { 1, 1, 2, 1, 3, 4 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << distribution(arr, n) << endl;
return 0;
}
Java
// Java program to equally divide n elements
// into two sets such that second set has
// maximum distinct elements.
import java.util.*;
class GFG
{
static int distribution(int arr[], int n)
{
Set resources = new HashSet();
// Insert all the resources in the set
// There will be unique resources in the set
for (int i = 0; i < n; i++)
resources.add(arr[i]);
// return minimum of distinct resources
// and n/2
return Math.min(resources.size(), n / 2);
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 1, 2, 1, 3, 4 };
int n = arr.length;
System.out.print(distribution(arr, n) +"\n");
}
}
// This code is contributed by Rajput-Ji
蟒蛇3
# Python3 program to equally divide n elements
# into two sets such that second set has
# maximum distinct elements.
def distribution(arr, n):
resources = set()
# Insert all the resources in the set
# There will be unique resources in the set
for i in range(n):
resources.add(arr[i]);
# return minimum of distinct resources
# and n/2
return min(len(resources), n // 2);
# Driver code
if __name__ == '__main__':
arr = [ 1, 1, 2, 1, 3, 4 ];
n = len(arr);
print(distribution(arr, n), "");
# This code is contributed by PrinciRaj1992
C#
// C# program to equally divide n elements
// into two sets such that second set has
// maximum distinct elements.
using System;
using System.Collections.Generic;
class GFG
{
static int distribution(int []arr, int n)
{
HashSet resources = new HashSet();
// Insert all the resources in the set
// There will be unique resources in the set
for (int i = 0; i < n; i++)
resources.Add(arr[i]);
// return minimum of distinct resources
// and n/2
return Math.Min(resources.Count, n / 2);
}
// Driver code
public static void Main(String[] args)
{
int []arr = { 1, 1, 2, 1, 3, 4 };
int n = arr.Length;
Console.Write(distribution(arr, n) +"\n");
}
}
// This code is contributed by PrinciRaj1992
Javascript
3
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。