#include <iostream> #include <map> using namespace std; int *pom; void merge(int tab[], int left, int midd, int right) { int i, j; for(i = midd + 1; i>left; i--){ pom[i-1] = tab[i-1]; } for(j = midd; j<right; j++) pom[right+midd-j] = tab[j+1]; for(int k=left;k<=right;k++) if(pom[j]>pom[i]) tab[k] = pom[j--]; else tab[k] = pom[i++]; } void mergeSort(int tab[],int left, int right) { if(right<=left) return; int midd = (right+left)/2; mergeSort(tab, left, midd); mergeSort(tab, midd+1, right); merge(tab, left, midd, right); } int main(int argc, char *argv[]) { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int n; cin>>n; int tab[n]; map<int, int> mp; for (int i=0; i<n; i++) { cin>>tab[i]; mp[tab[i]]++; } map<int, int>::iterator it = mp.begin(); int maxi = -1; int tmp = 0; int ile_roznych = mp.size(); int sortowanie[ile_roznych]; while (it != mp.end()) { if (it->second > maxi) { maxi = it->second; } sortowanie[tmp] = it->second; tmp++; ++it; } if (maxi > n/2) { cout<<1; return 0; } /*it = mp.begin(); while (it != mp.end()) { prio_queue.push(it->second); ++it; }*/ pom = new int[ile_roznych]; mergeSort(sortowanie, 0, ile_roznych - 1); /*for (int i=0; i<ile_roznych; i++) { cout<<sortowanie[i] << " "; }cout<<endl;*/ int result = 0; int i=0; int k=ile_roznych-1; int aktu; while(i<=k) { result++; aktu = sortowanie[k]; while (sortowanie[i] > aktu && i<k) { k--; aktu += sortowanie[k]; } i++; } cout<<result; 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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | #include <iostream> #include <map> using namespace std; int *pom; void merge(int tab[], int left, int midd, int right) { int i, j; for(i = midd + 1; i>left; i--){ pom[i-1] = tab[i-1]; } for(j = midd; j<right; j++) pom[right+midd-j] = tab[j+1]; for(int k=left;k<=right;k++) if(pom[j]>pom[i]) tab[k] = pom[j--]; else tab[k] = pom[i++]; } void mergeSort(int tab[],int left, int right) { if(right<=left) return; int midd = (right+left)/2; mergeSort(tab, left, midd); mergeSort(tab, midd+1, right); merge(tab, left, midd, right); } int main(int argc, char *argv[]) { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int n; cin>>n; int tab[n]; map<int, int> mp; for (int i=0; i<n; i++) { cin>>tab[i]; mp[tab[i]]++; } map<int, int>::iterator it = mp.begin(); int maxi = -1; int tmp = 0; int ile_roznych = mp.size(); int sortowanie[ile_roznych]; while (it != mp.end()) { if (it->second > maxi) { maxi = it->second; } sortowanie[tmp] = it->second; tmp++; ++it; } if (maxi > n/2) { cout<<1; return 0; } /*it = mp.begin(); while (it != mp.end()) { prio_queue.push(it->second); ++it; }*/ pom = new int[ile_roznych]; mergeSort(sortowanie, 0, ile_roznych - 1); /*for (int i=0; i<ile_roznych; i++) { cout<<sortowanie[i] << " "; }cout<<endl;*/ int result = 0; int i=0; int k=ile_roznych-1; int aktu; while(i<=k) { result++; aktu = sortowanie[k]; while (sortowanie[i] > aktu && i<k) { k--; aktu += sortowanie[k]; } i++; } cout<<result; return 0; } |