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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <vector>
#include <set>
#include <utility>

using namespace std;
typedef pair<int, int> para;

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);

	int n; cin >> n;
	vector<para> M(n + 1);

	for (int i = 0; i < n; ++i)
	{
		int x; cin >> x;
		++M[x].first;
	}

	auto cmp = [&M](int a, int b)
	{
		int sa = M[a].first + M[a].second;
		int sb = M[b].first + M[b].second;

		if (sa != sb) return sa < sb;
		return a < b;
	};

	auto cmpW = [&M](int a, int b)
	{
		int sa = M[a].first - M[a].second;
		int sb = M[b].first - M[b].second;

		if (sa != sb) return sa>sb;
		return a > b;
	};

	set<int, decltype(cmp)> S(cmp);
	set<int, decltype(cmpW)> SW(cmpW);

	for (int i = 1; i <= n; ++i)
	{
		if (M[i].first == 0) continue;
		S.insert(i);
		SW.insert(i);
	}

	while (S.size()>1)
	{
		int mini = *S.begin();
		int maks = *SW.begin();

		//if (mini == maks) break;

		int smin = M[mini].first + M[mini].second;
		int smax = M[maks].first - M[maks].second;

		int limit = smax - 1;
		bool przyciete = false;
		if (smin > limit)
		{
			smin = limit;
			przyciete = true;
		}

		if (limit <= 0) break;
		S.erase(S.begin());
		S.erase(maks);
		SW.erase(SW.begin());
		SW.erase(mini);

		M[mini].second -= smin;
		if (M[mini].second < 0)
		{
			M[mini].first += M[mini].second;
			M[mini].second = 0;
		}
		M[maks].second += smin;
		S.insert(maks);
		SW.insert(maks);
		if (przyciete)
		{
			S.insert(mini);
			SW.insert(mini);
		}
	}

	cout << S.size() << endl;

	return 0;
}