643div2 - B. Young Explorers

很容易想到贪婪,按inexperience 从小到大依次组肯定能获得最大组数。

此题有个问题,我用set TLE而map AC,难道两者速度差距这么大吗?求指点
map AC

#include
using namespace std;
const int MAXN = 2e5+10;
int T, N;
int e[MAXN];
map mii;
int main() {
    scanf("%d", &T);
    while(T--) {
        scanf("%d", &N);
        mii.clear();
        int e;
        for(int i=1; i<=N; i++) {
            scanf("%d", &e);
            mii[e]++;
        }
        int ans = 0; int rest = 0;
        for(auto e : mii) {
            ans +=(rest + e.second)/e.first;
            rest = (rest + e.second)%e.first;
        }        
        printf("%d\n", ans);
    }
    return 0;
}

set TLE

#include
using namespace std;
const int MAXN = 2e5+10;
int T, N;
int e[MAXN];
int cnt[MAXN];
set s;
int main() {
    scanf("%d", &T);
    while(T--) {
        scanf("%d", &N);
        memset(cnt, 0, sizeof(cnt));
        s.clear();
        int e;
        for(int i=1; i<=N; i++) {
            scanf("%d", &e);
            cnt[e]++;
            s.insert(e);
        }
        int ans = 0; int rest = 0;
        for(auto e : s) {
            ans += (rest + cnt[e])/e;
            rest = (rest + cnt[e])%e;
        }        
        printf("%d\n", ans);
    }
    return 0;
}

你可能感兴趣的:(643div2 - B. Young Explorers)