
使用正常循环时间会超时,使用桶排序则不会。桶的下标就是输入的数据,桶的值就是出现的次数。
- //桶排序
- #include
- #include
- using namespace std;
- using ll = long long;
- const int N = 2e5 + 9;
- ll c[N];
-
- int main()
- {
- ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
- int n; cin >> n;
- for (int i = 0; i < n; ++i)
- {
- int x; cin >> x;
- c[x]++;
- }
- for (int i = 0; i <= 2e5; ++i)
- {
- for (int j = 0; j < c[i]; ++j)
- {
- cout << i << " ";
- }
- }
-
- return 0;
- }