codeforces1077E.Thematic Contests(思維+STL)
E.Thematic Contests
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:
- 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 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 aiaiis the topic of the ii-th problem.
Output
Print one integer — the maximum number of problems in the set of thematic contests.
Examples
Input
18 2 1 2 10 2 10 10 2 2 1 10 10 10 10 1 1 10 10
Output
14
Input
10 6 6 6 3 6 1000000000 3 3 6 6
Output
9
Input
3 1337 1337 1337
Output
3
Note
In the first example the optimal sequence of contests is: 22 problems of the topic 11, 44 problems of the topic 22, 88 problems 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個問題,每種問題有一種型別ai,現在要舉辦一系列的比賽,在每場比賽中只能選取型別相同的題目,並且現在有要求,這一系列的比賽中,後面的比賽題目數量要是前一次比賽的兩倍。
詢問最多能選取多少題目作為比賽用。
三、大致思路
題目的型別其實是沒有用的,所以可以用map先處理出每種題目的數量。當晚想到這裡然後開始寫爆搜程式,TLE到結束。看了官方給出的題解,在這裡要列舉每次能取的最大的那種題目數量,然後不斷除二向前尋找可行的題目數,直到不滿足或者遍歷了全部的型別。
時間複雜度是O(nlogn)在這裡的n並不是指所有題目的數量,而是指數量最大的型別的題目。
四、程式碼
#include <iostream>
#include<string.h>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<set>
#include<map>
using namespace std;
typedef long long LL;
const LL inf=0x3f3f3f3f;
vector<int>q;
map<int,int>mmp;
int n,maxx;
int main()
{
scanf("%d", &n);
for (int i = 1;i <= n;i++)
{
int x;
scanf("%d", &x);
mmp[x]++;
}
map<int, int>::iterator it = mmp.begin(), en = mmp.end();
for (;it != en;it++)
{
maxx = max(maxx, it->second);
q.push_back(it->second);
}
sort(q.begin(),q.end());
int ans=0;
for(int i=1;i<=maxx;i++)
{
int sum=i;
int now=i;
int p=q.size()-1;
while(now%2==0&&p>0)
{
now/=2;
p--;
if(q[p]<now)break;
sum+=now;
}
ans=max(ans,sum);
}
printf("%d\n",ans);
}