1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
#include <algorithm>
#include <functional>

int main()
{
	static constexpr int MAX = 500'000;
	int input_count;
	scanf("%i", &input_count);

	int arr[MAX] = {0};
	for (int i=0; i< input_count; ++i) {
		int val;
		scanf("%i", &val);

		arr[val - 1] += 1;
	}

	std::sort(arr, arr + input_count, std::greater<int>{});

	int size = -1;
	int space = 0;
	for (int i=0; ; ++i) {
		if (arr[i] == 0) {
			size = i;
			break;
		}

		space += arr[i] - 1;
	}

	// std::cout << "size: " << size << "\n";
	// for (int i=0; i<size; ++i) {
	// 	std::cout << arr[i] << "\n";
	// }
	// std::cout << "space: " << space << "\n";

	int min_count = size;
	for (int i_=0; i_<size; ++i_) {
		int i = size - i_ - 1;

		if (arr[i] <= space - (arr[i] - 1)) {
			min_count -= 1;
			space -= arr[i] + (arr[i] - 1);
		}

		if (space == 0) {
			break;
		}
	}

	printf("%u\n", min_count);
	return 0;
}