#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;
vector<int> findMaxStamps(const vector<int>& stamps) {
int n = stamps.size();
unordered_map<int, int> cityCount;
for (int city : stamps) {
cityCount[city]++;
}
vector<int> maxStamps(n + 1, 0);
for (const auto& city : cityCount) {
for (int k = 1; k <= city.second; ++k) {
int sets = city.second / k;
maxStamps[k] += sets * k;
}
}
for (int k = n; k >= 1; --k) {
for (int j = 2*k; j <= n; j += k) {
maxStamps[k] = max(maxStamps[k], maxStamps[j]);
}
}
return maxStamps;
}
int main() {
int n;
cin >> n;
vector<int> stamps(n);
for (int i = 0; i < n; ++i) {
cin >> stamps[i];
}
vector<int> result = findMaxStamps(stamps);
for (int k = 1; k <= n; ++k) {
cout << result[k] << " ";
}
cout << endl;
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 | #include <iostream> #include <vector> #include <unordered_map> #include <algorithm> using namespace std; vector<int> findMaxStamps(const vector<int>& stamps) { int n = stamps.size(); unordered_map<int, int> cityCount; for (int city : stamps) { cityCount[city]++; } vector<int> maxStamps(n + 1, 0); for (const auto& city : cityCount) { for (int k = 1; k <= city.second; ++k) { int sets = city.second / k; maxStamps[k] += sets * k; } } for (int k = n; k >= 1; --k) { for (int j = 2*k; j <= n; j += k) { maxStamps[k] = max(maxStamps[k], maxStamps[j]); } } return maxStamps; } int main() { int n; cin >> n; vector<int> stamps(n); for (int i = 0; i < n; ++i) { cin >> stamps[i]; } vector<int> result = findMaxStamps(stamps); for (int k = 1; k <= n; ++k) { cout << result[k] << " "; } cout << endl; return 0; } |
English