【CodeForces 1077E --- Thematic Contests】思维+贪心
题目来源:点击进入【CodeForces 1077E — Thematic Contests】
Description
Polycarp has prepared n competitive programming problems. The topic of the i-th problem is ai, 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:
- number of problems in each contest is exactly twice as much as in the previous contest (one day ago), the first contest can contain arbitrary number of problems;
- the total number of problems in all the contests should be maximized.
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 n (1≤n≤2⋅105) — the number of problems Polycarp has prepared.
The second line of the input contains n integers a1,a2,…,an (1≤ai≤109) where ai is the topic of the i-th problem.
Output
Print one integer — the maximum number of problems in the set of thematic contests.
Sample Input
18
2 1 2 10 2 10 10 2 2 1 10 10 10 10 1 1 10 10
Sample Output
14
Note
In the first example the optimal sequence of contests is: 2 problems of the topic 1, 4 problems of the topic 2, 8 problems of the topic 10.
解题思路
利用arr[]数组存储第i种主题的问题个数。
将其排序后,从最大的开始向前推。依次更新ans。
AC代码:
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
using namespace std;
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define endl '\n'
const int MAXN = 2e5+5;
int arr[MAXN];
int main()
{
SIS;
int n,x,num=0,ans;
map<int,int> m;
cin >> n;
for(int i=0;i<n;i++)
{
cin >> x;
m[x]++;
}
for(auto i:m) arr[num++]=i.second;
sort(arr,arr+num);
ans=x=arr[num-1];
for(int i=num-2;i>=0;i--)
{
x=min(x/2,arr[i]);
if(!x) break;
ans=max(ans,((1<<(num-i))-1)*x);
}
cout << ans << endl;
return 0;
}