TLE求助
查看原帖
TLE求助
729048
luojien楼主2024/2/29 20:31
#include <iostream>
using namespace std;
const int N = 1e6 + 5;
int cnt[N];
int main(){
    int n;
    cin >> n;
    int maxw = -1;
    for(int i = 0; i < n; i++){
        int w;
        cin >> w;
        cnt[w]++;
        maxw = max(maxw, w);
    }
    int res = 0;
    for(int i = 0; i <= maxw || cnt[i]; i++){
        if(cnt[i] == 0){
            continue;
        }
        cnt[i + 1] += cnt[i] / 2;
        cnt[i] %= 2;
        if(cnt[i] > 0){
            res++;
        }
    }
    cout << res << endl;
    return 0;
}

按照题解的方法写的,但是无缘无故tle了。

2024/2/29 20:31
加载中...