跳到正文

个人记录

sss

AmikazeMinato约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年8月6日
收录
2025年12月12日
更新
2026年11月9日
篇幅
约 2 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h> 
#define int long long
using namespace std;

int solve() {
    int n;
    cin >> n;
    vector<int> a(n);
    map<int, int> freq;

    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        freq[a[i]]++;
    }
    
    sort(a.begin(), a.end());
    
    int res = 0;
    for (auto it = freq.begin(); it != freq.end(); ++it) {
        int x = it->first;
        int cnt = it->second;
        
        if (freq.find(x - 1) == freq.end()) {
           
            res += cnt;
        } else {
            
            if (cnt > freq[x - 1]) {
                res += cnt - freq[x - 1];
            }
        }
    }
    
    return res;
}

signed main() {
    int T;
    cin >> T;
    while (T--) {
        cout << solve() << '\n';
    }
    return 0;
}
原文