参考铺设道路,我们对于每一个 x 计算 cnt[x]-cnt[x-1] 就做完了

使用哈希表可以避免排序复杂度

__gnu_pbds::gp_hash_table 比 std::unordered_map 快,并使用 splitmix64 防造数据卡

#include <cctype>
#include <chrono>
#include <cstdio>
#include <iostream>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM =
            chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

__gnu_pbds::gp_hash_table<int, int, custom_hash> cnt;

int Read() {
    char c = getchar_unlocked();
    int res = 0;
    while (!isdigit(c)) {
        c = getchar_unlocked();
    }
    while (isdigit(c)) {
        res = res * 10 + (c - '0');
        c = getchar_unlocked();
    }
    return res;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n = Read();
    int res = 0;
    while (n--) {
        cnt[Read()]++;
    }
    for (const auto& [k, v] : cnt) {
        if (cnt.find(k - 1) != cnt.end()) {
            res += max(0, v - cnt[k - 1]);
        } else {
            res += v;
        }
    }
    cout << res;
}
// 64 位输出请用 printf("%lld")