本文共 2894 字,大约阅读时间需要 9 分钟。
This problem is actually a subproblem of problem G from the same contest.
There are nn candies in a candy box. The type of the ii-th candy is aiai (1≤ai≤n).
You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type 1 and two candies of type 2 is bad).
It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.
Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have.
You have to answer qq independent queries.
If you are Python programmer, consider using Py instead of Python when you submit your code.
Input
The first line of the input contains one integer qq (1≤q≤2⋅105) — the number of queries. Each query is represented by two lines.
The first line of each query contains one integer nn (1≤n≤2⋅105) — the number of candies.
The second line of each query contains nn integers a1,a2,…,an (1≤ai≤n), where ai is the type of the ii-th candy in the box.
It is guaranteed that the sum of nn over all queries does not exceed 2⋅105.
Output
For each query print one integer — the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement.
注意!!:我想大家应该大部分是来找TLE的原因的,经过我本人的n次提交,最终我终于找到了bug所在,那就是memset初始化数组,在这比for循环慢!下面贴上两种AC代码。
第一种耗时93ms,一开始用memset初始化a数组时一直TLE第27组数据。
#includeusing namespace std;int a[200010];priority_queue ,less > p;int main(){ int q,n,t; scanf("%d",&q); while(q--) { int sum=0; scanf("%d",&n); for(int i=1;i<=n;i++) { a[i]=0; } int maxx=-1e5; for(int i=1; i<=n; i++) { scanf("%d",&t); a[t]++; maxx=max(maxx,t); } for(int i=1; i<=maxx; i++) if(a[i]!=0) p.push(a[i]); int j=2e5+5; while(!p.empty()) { j=min(p.top(),j); sum+=j; j--; p.pop(); if(j<=0) break; } while(!p.empty()) p.pop(); printf("%d\n",sum); } return 0;}
第二种是让我找到bug所在的代码...耗时841ms...
#includeusing namespace std;const int maxn=2e5+5;int q,n;int a[maxn];int main(){ cin>>q; while(q--) { cin>>n; for(int i=1; i<=n; i++) { a[i]=0; } for(int i=1; i<=n; i++) { int t=0; cin>>t; a[t]++; } sort(a+1,a+n+1); int sum=0; for(int i=n,j=a[n]; i>=1&&j>0; sum+=j--,i--) { j=min(j,a[i]); } cout< <
各位大佬不喜勿喷
转载地址:http://fhtgn.baihongyu.com/