fork(5) download
  1. #include<iostream>
  2. #include<map>
  3. #include<vector>
  4. #include<iterator>
  5. #include<algorithm>
  6.  
  7. using namespace std;
  8.  
  9. struct num {
  10. int val, count, index;
  11. num(int v, int c, int i) {
  12. val=v;
  13. count=c;
  14. index=i;
  15. }
  16. };
  17.  
  18. bool operator < (num a, num b) {
  19. if(a.count==b.count) {
  20. return a.index<b.index;
  21. }
  22. else return a.count>b.count;
  23. }
  24.  
  25. int main () {
  26. int n,c;
  27. //freopen("input.txt","r",stdin);
  28. map <int , pair<int,int> > m;
  29. scanf("%d %d",&n,&c);
  30. int temp;
  31. for(int i=0;i<n;i++) {
  32. scanf("%d",&temp);
  33. map <int, pair <int,int> >::iterator it = m.find(temp);
  34. if(it==m.end()) {
  35. m[temp]=make_pair(i,1);
  36. }
  37. else {
  38. it->second.second++;
  39. }
  40. }
  41. vector <num> v;
  42. for(map< int,pair<int,int> >::iterator it=m.begin();it!=m.end();it++) {
  43. // cout<<it->first<<" "<<it->second.first<<" "<<it->second.second<<endl;
  44. v.push_back(num(it->first,it->second.second,it->second.first));
  45. }
  46. sort(v.begin(), v.end());
  47. for(vector <num>::iterator it=v.begin();it!=v.end();it++) {
  48. for(int i=0;i<it->count;i++) {
  49. printf("%d ",it->val);
  50. }
  51. }
  52. printf("\n");
  53. return 0;
  54. }
Success #stdin #stdout 0s 3476KB
stdin
7 3
2 3 3 3 2 1 2
stdout
2 2 2 3 3 3 1