#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <map>
#include <algorithm>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
vector<int> integers;
int n;
cin >> n;
cin.ignore();
string input;
getline(cin, input);
istringstream iss(input);
int num;
while (iss >> num) {
integers.push_back(num);
}
// Count occurrences
map<int, int> occurrences;
for (int numb : integers) {
occurrences[numb]++;
}
int arr[occurrences.size()];
int i = 0;
for ( const auto &p : occurrences ) {
arr[i] = p.second;
i++;
}
int len = sizeof(arr)/sizeof(arr[0]);
sort(arr, arr + len, greater<int>());
if(2 * arr[0] > n) {
cout << "1\n";
return 0;
}
int amountOfSubSequences = 0;
int j = len - 1;
for(int i = 0; i < len; i++) {
int val = arr[i];
if(val <= arr[j]) {
cout << (len - amountOfSubSequences) << "\n";
return 0;
}
while(val > arr[j]) {
val -= arr[j];
// cout << val << endl;
j--;
amountOfSubSequences += 1;
}
if (i == 0) {
if(j == len - 1) {
cout << len << "\n";
return 0;
}
}
}
cout << (len - amountOfSubSequences) << "\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 | #include <iostream> #include <string> #include <vector> #include <sstream> #include <map> #include <algorithm> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); vector<int> integers; int n; cin >> n; cin.ignore(); string input; getline(cin, input); istringstream iss(input); int num; while (iss >> num) { integers.push_back(num); } // Count occurrences map<int, int> occurrences; for (int numb : integers) { occurrences[numb]++; } int arr[occurrences.size()]; int i = 0; for ( const auto &p : occurrences ) { arr[i] = p.second; i++; } int len = sizeof(arr)/sizeof(arr[0]); sort(arr, arr + len, greater<int>()); if(2 * arr[0] > n) { cout << "1\n"; return 0; } int amountOfSubSequences = 0; int j = len - 1; for(int i = 0; i < len; i++) { int val = arr[i]; if(val <= arr[j]) { cout << (len - amountOfSubSequences) << "\n"; return 0; } while(val > arr[j]) { val -= arr[j]; // cout << val << endl; j--; amountOfSubSequences += 1; } if (i == 0) { if(j == len - 1) { cout << len << "\n"; return 0; } } } cout << (len - amountOfSubSequences) << "\n"; return 0; } |
English