CodeForces 1183D. Candy Box (easy version)

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 <bits/stdc++.h>

using namespace std;

int a[200010];
priority_queue<int,vector<int>,less<int> > 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<bits/stdc++.h>
 
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<<sum<<endl;
    }
}

各位大佬不喜勿喷

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值