#include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> #include <unordered_map> #include <vector> #include <string> #include <algorithm> int main(){ int n; int x; scanf("%d", &n); std::vector<char> base(n); std::unordered_map<int, int> big; for(int i=0; i<n; i++) { scanf(" %d", &x); if (big.find(x) != big.end()) { big[x]++; } else { if(base[x - 1] == 255) { big[x] = 256; base[x - 1] = 0; } else { base[x - 1]++; } } } std::vector<int> values; for (const auto& pair : big) { values.push_back(pair.second); } std::sort(values.begin(), values.end(), std::greater<int>()); int sum = 0; int result = 0; for (int value : values) { sum += (((value - 1)*2)+1); result++; if(sum >= n) { printf("%d", result); return 0; } } std::sort(base.begin(), base.end(), std::greater<int>()); for (int value : base) { sum += (((value - 1)*2)+1); result++; if(sum >= n) { printf("%d", result); return 0; } } printf("%d", 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 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> #include <unordered_map> #include <vector> #include <string> #include <algorithm> int main(){ int n; int x; scanf("%d", &n); std::vector<char> base(n); std::unordered_map<int, int> big; for(int i=0; i<n; i++) { scanf(" %d", &x); if (big.find(x) != big.end()) { big[x]++; } else { if(base[x - 1] == 255) { big[x] = 256; base[x - 1] = 0; } else { base[x - 1]++; } } } std::vector<int> values; for (const auto& pair : big) { values.push_back(pair.second); } std::sort(values.begin(), values.end(), std::greater<int>()); int sum = 0; int result = 0; for (int value : values) { sum += (((value - 1)*2)+1); result++; if(sum >= n) { printf("%d", result); return 0; } } std::sort(base.begin(), base.end(), std::greater<int>()); for (int value : base) { sum += (((value - 1)*2)+1); result++; if(sum >= n) { printf("%d", result); return 0; } } printf("%d", n); return 0; } |