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
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
#include <iostream>
#include <unordered_map>
#include <vector>
#include <algorithm>

void printVec(std::vector<int> &frequencies, int start, int end)
{
    // std::clog << "freq: ";
    for (auto i = 0; i < frequencies.size(); i++)
    {
        if (i == start || i == end)
        {
            // std::clog << "[";
        }
        // std::clog << frequencies[i];
        if (i == start || i == end)
        {
            // std::clog << "]";
        }
        // std::clog << " ";
    }
    // std::clog << std::endl;
}

int solve(std::vector<int> &nums)
{
    std::unordered_map<int, int> frequencyMap;
    for (int num : nums)
    {
        ++frequencyMap[num];
    }

    std::vector<int> frequencies(frequencyMap.size());
    std::transform(frequencyMap.begin(), frequencyMap.end(), frequencies.begin(), [](const auto &pair)
                   { return pair.second; });

    sort(frequencies.begin(), frequencies.end(), std::greater<int>());

    int subsequences = 0;
    int start = 0;
    int end = frequencies.size() - 1;
    int count = 0;

    while (start < end)
    {
        int leader = frequencies[start];
        count = 0;

        while (start < end)
        {
            // printVec(frequencies, start, end);

            if (leader == frequencies[end] && leader == 1)
            {
                subsequences += end - start + 1;
                start = end;
                break;
            }
            int limit = leader - 1;
            count += frequencies[end];

            if (count >= limit)
            {
                int remainder = count - limit;
                ++subsequences;
                frequencies[end] -= remainder;
                if (remainder == 0)
                {
                    --end;
                }
                count = 0;
                break;
            }
            --end;
        }

        if (count != 0)
        {
            ++subsequences;
            break;
        }
        ++start;
    }

    if (start == end && count == 0)
    {
        ++subsequences;
    }

    return subsequences;
}

int main()
{
    int n;
    std::cin >> n;
    std::vector<int> nums(n);
    for (int i = 0; i < n; ++i)
    {
        std::cin >> nums[i];
    }
    std::cout << solve(nums) << std::endl;
    return 0;
}