#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
bool compareByValueDesc(const std::pair<int, int>& a, const std::pair<int, int>& b) {
return a.second > b.second;
}
int main() {
int n;
cin >> n;
map<int, int> store;
for (int i = 0; i < n; i++) {
int k;
cin >> k;
store[k]++;
}
// Copy the elements to a vector
vector<pair<int, int>> myList(store.begin(), store.end());
// Sort the vector based on the key (first element of the pair)
sort(myList.begin(), myList.end(), compareByValueDesc);
int i = 0, j = myList.size()-1;
int counter = 0;
while (i <= j) {
int toDecrease = myList[i].second - 1;
i++;
while (toDecrease > 0 && i <= j ) {
if (myList[j].second <= toDecrease) {
toDecrease -= myList[j].second;
j--;
} else {
myList[j].second -= toDecrease;
toDecrease = 0;
}
}
counter++;
}
cout << counter << endl;
}
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 | #include <iostream> #include <map> #include <vector> #include <algorithm> using namespace std; bool compareByValueDesc(const std::pair<int, int>& a, const std::pair<int, int>& b) { return a.second > b.second; } int main() { int n; cin >> n; map<int, int> store; for (int i = 0; i < n; i++) { int k; cin >> k; store[k]++; } // Copy the elements to a vector vector<pair<int, int>> myList(store.begin(), store.end()); // Sort the vector based on the key (first element of the pair) sort(myList.begin(), myList.end(), compareByValueDesc); int i = 0, j = myList.size()-1; int counter = 0; while (i <= j) { int toDecrease = myList[i].second - 1; i++; while (toDecrease > 0 && i <= j ) { if (myList[j].second <= toDecrease) { toDecrease -= myList[j].second; j--; } else { myList[j].second -= toDecrease; toDecrease = 0; } } counter++; } cout << counter << endl; } |
English