1335C - Two Teams Composing - CodeForces Solution


binary search greedy implementation sortings *1100

Please click on ads to support us..

Python Code:

t = int(input())

for _ in range(t):
    n = int(input())
    a = list(map(int,input().split()))
    cnt=[0]*(len(a)+1)
    for x in a:
        cnt[x]+=1
        
    l = len(set(a))
    m = max(cnt)
    if l==m:
        print(m-1)
    else:
        print(min(l,m))

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define fast() ios_base::sync_with_stdio(0); cin.tie(0)
#define vec vector<ll>
#define pb  push_back
#define alls(s) s.begin(),s.end()
#define allv(v) v.begin(),v.end()
#define allv2(v2) v2.begin(),v2.end()
#define allrs(s) s.rbegin(),s.rend()
#define allrv(v) v.rbegin(),v.rend()
#define allrv2(v2) v2.rbegin(),v2.rend()
#define f1(i,t) for(int i=0;i<t;i++)
#define f2(i,n) for(int i=0;i<n;i++)
#define f3(j,t) for(int j=0;j<t;j++)
#define f4(j,t) for(int j=0;j<n;j++)
#define fs1(i,s) for(int i=0;i<s.size();i++)
#define fs2(i,s2) for(int i=0;i<s2.size();i++)
typedef long long ll;
int main()
{
  fast();
  ll t,n;
  cin>>t;
  while(t--)
  {
    cin>>n;
    vec v(n);
    f2(i,n)
    {
      cin>>v[i];
    }
    map<ll,ll>mp;
    vector<pair<ll,ll>>v2;
    f2(i,n)
    {
      mp[v[i]]++;
      auto g=make_pair(mp[v[i]],v[i]);
      v2.pb(g);
    }
   sort(allrv2(v2));
   sort(allv(v));
   auto h=unique(allv(v));
   v.erase(h,v.end());
   ll kk=v.size();
   cout<<max(min(kk-1,v2[0].first),min(kk,v2[0].first-1))<<endl;
  }
}


Comments

Submit
0 Comments
More Questions

48A - Rock-paper-scissors
294A - Shaass and Oskols
1213A - Chips Moving
490A - Team Olympiad
233A - Perfect Permutation
1360A - Minimal Square
467A - George and Accommodation
893C - Rumor
227B - Effective Approach
1534B - Histogram Ugliness
1611B - Team Composition Programmers and Mathematicians
110A - Nearly Lucky Number
1220B - Multiplication Table
1644A - Doors and Keys
1644B - Anti-Fibonacci Permutation
1610A - Anti Light's Cell Guessing
349B - Color the Fence
144A - Arrival of the General
1106A - Lunar New Year and Cross Counting
58A - Chat room
230A - Dragons
200B - Drinks
13A - Numbers
129A - Cookies
1367B - Even Array
136A - Presents
1450A - Avoid Trygub
327A - Flipping Game
411A - Password Check
1520C - Not Adjacent Matrix