求问做法正确性
查看原帖
求问做法正确性
833737
Lyw_and_Segment_Tree楼主2024/10/26 21:08

rt,出考场的时候和同学对答案,然后发现同学们都写的众数做法,包括洛谷上也是,求问我的做法正确性。目前洛谷民间数据 100 pts, code :

#include <bits/stdc++.h>
#define ll long long
#define endl "\n"

using namespace std;

ll n, a[114514], cnt[114514], tot[114514], ans = 0;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin>> n;
    for (ll i = 1; i <= n; i++) {
        cin >> a[i]; cnt[a[i]] ++, tot[a[i]] ++;
    }
    sort(a + 1, a + n + 1); ll sz = unique(a + 1, a + n + 1) - a - 1;
    for (ll i = 1; i <= sz; i++) {
        ll pos = i - 1;
        while (cnt[a[i]] >= 0 && pos >= 1) {
            if (cnt[a[i]] >= tot[a[pos]]) {
                cnt[a[i]] -= tot[a[pos]], tot[a[pos]] = cnt[a[pos]] = 0;
            } else {
                tot[a[pos]] -= cnt[a[i]], cnt[a[i]] = 0;
            }
            pos --;
        }
    }
    for (ll i = 1; i <= sz; i++) {
        ans += tot[a[i]];
    }
    cout << ans << endl;
}
2024/10/26 21:08
加载中...