https://codeforc.es/problemset/problem/1077/E
E. Thematic Contests
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Polycarp has prepared nn competitive programming problems. The topic of the ii-th problem is aiai, and some problems' topics may coincide.
Polycarp has to host several thematic contests. All problems in each contest should have the same topic, and all contests should have pairwise distinct topics. He may not use all the problems. It is possible that there are no contests for some topics.
Polycarp wants to host competitions on consecutive days, one contest per day. Polycarp wants to host a set of contests in such a way that:
Your task is to calculate the maximum number of problems in the set of thematic contests. Note, that you should not maximize the number of contests.
Input
The first line of the input contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of problems Polycarp has prepared.
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) where aiai is the topic of the ii-th problem.
Output
Print one integer — the maximum number of problems in the set of thematic contests.
Examples
input
Copy
18 2 1 2 10 2 10 10 2 2 1 10 10 10 10 1 1 10 10
output
Copy
14
input
Copy
10 6 6 6 3 6 1000000000 3 3 6 6
output
Copy
9
input
Copy
3 1337 1337 1337
output
Copy
3
Note
In the first example the optimal sequence of contests is: 22 problems of the topic 11, 44 problems of the topic 22, 88problems of the topic 1010.
In the second example the optimal sequence of contests is: 33 problems of the topic 33, 66 problems of the topic 66.
In the third example you can take all the problems with the topic 13371337 (the number of such problems is 33 so the answer is 33) and host a single contest.
题意:有n个问题,每个问题都属于某个话题,现在要举行一系列比赛,每个比赛只能用同种类的问题而且问题数是前一场比赛的2倍(第一场比赛问题数可以任意),要你安排比赛使比赛总问题数最大
#include
using namespace std;
#define ll long long
//#define mp make_pair
#define CL(a, b) memset(a, b, sizeof(a))
#define debug(x) cout<<"debug"< mp;
int main() {
int n;
scanf ("%d", &n);
int cnt = 0, x;
for(int i = 1; i <= n; ++i) {
scanf ("%d", &x);
if (!mp[x]) {
mp[x] = ++cnt;
}
++a[mp[x]]; //x这个话题的问题数加一
}
//for (int i = 1; i <= cnt; ++i) printf("%d ", a[i]);
//puts("");
sort(a+1, a+cnt+1);
//for (int i = 1; i <= cnt; ++i) printf("%d ", a[i]);
//puts("");
int ans = 0;
for(int j = 1; j <= n; ++j) { //j为枚举第一次contest可能的问题数
//j的最大值为当每个问题的topic一样的时候,取n
int m = 0;
// 暴力做法
/*for (int i = 1, k = j; i <= cnt; ++i) {
if (a[i] >= k) {
m += k;
k *= 2;
}
}*/
// 二分加速
int la = 1;
for (int k = j; k <= n; k *= 2) { //k为每次contest的问题数
int pos = lower_bound(a+la, a+cnt+1, k) - a;
if (pos == cnt+1) break; //如果没有比k大的话题相同的问题就break
m += k; //否则继续举行比赛
la = pos + 1;
}
ans = max(ans, m); //更新答案
}
printf ("%d\n", ans);
return 0;
}