// Author: Bartek Knapik #include <cstdio> const int MAX_N = 300000 + 9; int stamps[MAX_N], tmp[MAX_N], counter[MAX_N]; int n, ans, n_cities; void sort(int* data, int begin, int end) { if (end - begin <= 1) return; int mid = (begin + end) / 2; sort(data, begin, mid); sort(data, mid, end); int a = begin; int b = mid; int c = 0; while (a < mid && b < end) { if (data[a] > data[b]) tmp[c++] = data[a++]; else tmp[c++] = data[b++]; } while (a < mid) tmp[c++] = data[a++]; while (b < end) tmp[c++] = data[b++]; c = 0; while (begin < end) data[begin++] = tmp[c++]; } void get_counter() { int prev_val, count; prev_val = stamps[0]; count = 1; n_cities = 0; for (int i = 1; i < n; ++i) { if (prev_val != stamps[i]) { counter[n_cities++] = count; count = 1; prev_val = stamps[i]; } else count++; } counter[n_cities++] = count; } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%d", &stamps[i]); sort(stamps, 0, n); get_counter(); sort(counter, 0, n_cities); for (int i = 1; i <= n; ++i) { ans = 0; for (int j = 0; j < n_cities; ++j) { ans += (counter[j] / i) * i; if (counter[j] / i == 0) n_cities = j; } printf("%d ", ans); } printf("\n"); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | // Author: Bartek Knapik #include <cstdio> const int MAX_N = 300000 + 9; int stamps[MAX_N], tmp[MAX_N], counter[MAX_N]; int n, ans, n_cities; void sort(int* data, int begin, int end) { if (end - begin <= 1) return; int mid = (begin + end) / 2; sort(data, begin, mid); sort(data, mid, end); int a = begin; int b = mid; int c = 0; while (a < mid && b < end) { if (data[a] > data[b]) tmp[c++] = data[a++]; else tmp[c++] = data[b++]; } while (a < mid) tmp[c++] = data[a++]; while (b < end) tmp[c++] = data[b++]; c = 0; while (begin < end) data[begin++] = tmp[c++]; } void get_counter() { int prev_val, count; prev_val = stamps[0]; count = 1; n_cities = 0; for (int i = 1; i < n; ++i) { if (prev_val != stamps[i]) { counter[n_cities++] = count; count = 1; prev_val = stamps[i]; } else count++; } counter[n_cities++] = count; } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%d", &stamps[i]); sort(stamps, 0, n); get_counter(); sort(counter, 0, n_cities); for (int i = 1; i <= n; ++i) { ans = 0; for (int j = 0; j < n_cities; ++j) { ans += (counter[j] / i) * i; if (counter[j] / i == 0) n_cities = j; } printf("%d ", ans); } printf("\n"); return 0; } |