Codeforces Round #634 C. Two Teams Composing(排序)

题目描述

You have n n n students under your control and you have to compose exactly two teams consisting of some subset of your students. Each student had his own skill, the i i i -th student skill is denoted by an integer ai a_i ai (different students can have the same skills).

So, about the teams. Firstly, these two teams should have the same size. Two more constraints:

  • The first team should consist of students with distinct skills (i.e. all skills in the first team are unique).
  • The second team should consist of students with the same skills (i.e. all skills in the second team are equal).

Note that it is permissible that some student of the first team has the same skill as a student of the second team.

Consider some examples (skills are given):

  • [1,2,3] [1, 2, 3] [1,2,3] , [4,4] [4, 4] [4,4] is not a good pair of teams because sizes should be the same;
  • [1,1,2] [1, 1, 2] [1,1,2] , [3,3,3] [3, 3, 3] [3,3,3] is not a good pair of teams because the first team should not contain students with the same skills;
  • [1,2,3] [1, 2, 3] [1,2,3] , [3,4,4] [3, 4, 4] [3,4,4] is not a good pair of teams because the second team should contain students with the same skills;
  • [1,2,3] [1, 2, 3] [1,2,3] , [3,3,3] [3, 3, 3] [3,3,3] is a good pair of teams;
  • [5] [5] [5] , [6] [6] [6] is a good pair of teams.

Your task is to find the maximum possible size x x x for which it is possible to compose a valid pair of teams, where each team size is x x x (skills in the first team needed to be unique, skills in the second team should be the same between them). A student cannot be part of more than one team.

You have to answer t t t independent test cases.

输入格式

The first line of the input contains one integer t t t ( 1≤t≤104 1 \le t \le 10^4 1t104 ) — the number of test cases. Then t t t test cases follow.

The first line of the test case contains one integer n n n ( 1≤n≤2⋅105 1 \le n \le 2 \cdot 10^5 1n2105 ) — the number of students. The second line of the test case contains n n n integers a1,a2,…,an a_1, a_2, \dots, a_n a1,a2,,an ( 1≤ai≤n 1 \le a_i \le n 1ain ), where ai a_i ai is the skill of the i i i -th student. Different students can have the same skills.

It is guaranteed that the sum of n n n over all test cases does not exceed 2⋅105 2 \cdot 10^5 2105 ( ∑n≤2⋅105 \sum n \le 2 \cdot 10^5 n2105 ).

输出格式

For each test case, print the answer — the maximum possible size x x x for which it is possible to compose a valid pair of teams, where each team size is x x x .

输入输出样例

输入 #1
4
7
4 2 4 1 4 3 4
5
2 1 5 4 3
1
1
4
1 1 1 3
输出 #1
3
1
0
2
sort以后求出有多少不同的数diff以及出现最多的数的出现次数mmax,最终答案是max(min(mmax-1,diff),min(mmax,diff-1))因为可以这么分(1 1 1 1)(2 3 4) 也可以这么分(1 1 1)(1 2 3 4)这两种情况取最大即可。
#include <bits/stdc++.h>
using namespace std;
int a[200005];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,i;
        cin>>n;
        int pre=0,cnt=0,mmax=0,diff=0;//mmax:出现次数最多的数的次数 diff:多少个不同的数 cnt:当前数出现的次数 pre:上一个数 
        for(i=1;i<=n;i++) scanf("%d",&a[i]);
        sort(a+1,a+n+1);
        for(i=1;i<=n+1;i++)//得多写一个 
        {
            if(a[i]!=pre)
            {
                pre=a[i];
                diff++;
                mmax=max(mmax,cnt);
                cnt=1;
            }
            else cnt++;
        }
        diff--;//减去多的那个 
        cout<<max(min(mmax-1,diff),min(mmax,diff-1))<<endl;
    }
    return 0;
}
扫描二维码关注公众号,回复: 10775852 查看本文章

猜你喜欢

转载自www.cnblogs.com/lipoicyclic/p/12695861.html