Codeforces Round #570 (Div. 3)D. Candy Box (easy version)

原题出处:http://codeforces.com/contest/1183/problem/D

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≤n1≤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 11 and two candies of type 22 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 PyPy instead of Python when you submit your code.

Input

The first line of the input contains one integer qq (1≤q≤2⋅1051≤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⋅1051≤n≤2⋅105) — the number of candies.

The second line of each query contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤ai≤n), where aiai 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⋅1052⋅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.

Example

input

Copy

3
8
1 4 8 4 5 6 3 8
16
2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1
9
2 2 4 4 4 7 7 7 7

output

Copy

3
10
9

Note

In the first query, you can prepare a gift with two candies of type 88 and one candy of type 55, totalling to 33 candies.

Note that this is not the only possible solution — taking two candies of type 44 and one candy of type 66 is also valid.

    这题本来是一个简单题,但是不可以开数组来解决,开数组需要把数组的大小开到2*10^5,当很
q大并且n很大时,使用sort排序时会TLE。所以这里我们使用C++中的vector容器构建一个长度为
n+1的向量列解决这个问题,这就避免了每次在读取数据时需要对20000个数据进行排序,而只需
对我们读取的数据进行排序,因此不会超时。

    具体做法:维护一个长度为n+1的向量,长度为n+1是因为数据ai范围为1≤ai≤n,然后对该向量进行
模拟,定义中间变量ans和总和sum,设置ans=cnt[0]的原因是至少可以在礼物中准备cnt[0]个糖
果,所以sum的初始值也设置为cnt[0],在模拟的过程中,当cnt[i]>=ans时,第一次模拟的时候
cnt[i]只可能等于ans,因为对cnt进行了从大到小的排序,cnt[i+1]<=cnt[i],但随着模拟的进
行,ans的减小,cnt[i]会大于ans的值,这就相当于我有四种种类的糖果,每种糖果的都有4个,
在第一次取4个后,为满足题目要求,后面几种只能取3,2,1种了,而当cnt[i]小于ans时,我们直
接可以加上cnt[i],这时候我们需要对ans进行更新,使得ans=cnt[i]。

 

#include<bits/stdc++.h>
#include<algorithm>
 
using namespace std;

int cmp(int x,int y)
{
	return x > y;
}
 
int main()
{
	int t;
	scanf("%d",&t);
	while (t--)
	{
		int n;
		scanf("%d",&n);
		vector<int> a(n+1);
		for (int i=0;i<n;i++)
		{
			int x;
			scanf("%d",&x);
			a[x]++;
		}
		sort(a.begin(),a.end(),cmp);
		int cnt = a[0];
		int sum = a[0];
		for (int i=1;i<n;i++)
		{
			if (cnt==0)
				break;
			else if (a[i]>=cnt)
			{
				cnt--;
				sum += cnt;
			}
			else
			{
				cnt = a[i];
				sum += cnt;
			}
		}
		printf("%d\n",sum);
	}
	return 0;
}
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值