ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 1335-C, *1100
    코드포스 2021. 7. 23. 21:27
     

    Problem - 1335C - Codeforces

     

    codeforces.com

    문제

    • $n(1 \leq n \leq 2 \cdot 10^{5})$개의 수 중 일부를 사용하여
      크기가 같은 두 개의 집합 $A,\ B$을 구성한다, $\left | A \right | = \left | B \right |$
    • $A$에는 같은 수가 들어가면 안된다
    • $B$에는 같은 수만 들어가야 한다
    • $\left | A \right | = \left | B \right |$를 최대로 구성할 때 집합의 크기는?
    • $O(nlog\ n)$ 정도의 알고리즘은 통과할 수 있다

     

    $O(nlog\ n)$

    수의 개수를 세서 map에 보관하자 $O(nlog\ n)$

    3 5 7 8
    개수 1 2 3 4 $=\mathrm{maxval}$

    i) $\mathrm{map.size}<\mathrm{maxval}$ ~ $\mathrm{map.size}$
    ii) $\mathrm{map.size}=\mathrm{maxval}$ ~ $\mathrm{map.size-1}$
    iii) $\mathrm{map.size}>\mathrm{maxval}$ ~ $\mathrm{maxval}$

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    #include <bits/stdc++.h>
    #define endl "\n"
    using namespace std;
     
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0), cout.tie(0);
     
        int t;
        cin >> t;
        while(t--){
            int n;
            cin >> n;
            map<intint> m;
            m.clear();
            int tmp;
     
            while(n--){
                cin >> tmp;
                m[tmp]++;
            }
     
            int max_num = 0;
            for(auto e: m) max_num = max(max_num, e.second);
     
            if(m.size() < max_num) cout << m.size() << endl;
            else if(m.size() == max_num) cout << m.size()-1 << endl;
            else cout << max_num << endl;
     
        }
     
     
        return 0;

     

    '코드포스' 카테고리의 다른 글

    467-B, *1100  (0) 2021.07.24
    1327-A, *1100  (0) 2021.07.24
    368-B, *1100  (0) 2021.07.23
    313-B, *1100  (0) 2021.07.23
    456-A, *1100  (0) 2021.07.23

    댓글

Designed by Tistory.