博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CodeForces 1183D. Candy Box (easy version)
阅读量:3929 次
发布时间:2019-05-23

本文共 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组数据。

#include 
using 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...

#include
using 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/

你可能感兴趣的文章
JAVA算法:DFS算法题解两个例子(走迷宫和求排列组合数)
查看>>
LeetCode刷题:93. Restore IP Addresses
查看>>
JAVA算法:DFS算法求解素数环问题
查看>>
LeetCode刷题:39. Combination Sum
查看>>
LeetCode刷题:200. Number of Islands
查看>>
LeetCode刷题:78. Subsets
查看>>
LeetCode刷题:695. Max Area of Island(JAVA代码详解)
查看>>
LeetCode刷题:657. Judge Route Circle
查看>>
LeetCode刷题:5. Longest Palindromic Substring
查看>>
研发主管的烦恼:EBS-6号服务器宕机引发的“血案”
查看>>
PMP笔记—PMP考试中不同合同类型的优缺点比较
查看>>
PMP笔记—PMP考试中合同类型选择判断技巧
查看>>
精益Scrum(一)
查看>>
精益Scrum(二)
查看>>
精益Scrum(三)
查看>>
精益Scrum(四)
查看>>
精益Scrum(五)
查看>>
精益Scrum(六)
查看>>
精益Scrum(七)
查看>>
软件测试管理—如何写好软件测试计划书
查看>>