fork download
  1. #include <iostream>
  2. #include <unordered_map>
  3. #include <algorithm>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. typedef long long ll;
  9.  
  10. struct frequency{
  11. int count = 1;
  12. int initial;
  13. };
  14.  
  15. bool cmp(pair<int, frequency> f1, pair<int, frequency> f2){
  16. if(f1.second.count > f2.second.count) return true;
  17. else if(f1.second.count==f2.second.count) {
  18. if(f1.second.initial < f2.second.initial) return true;
  19. else return false;
  20. }else{
  21. return false;
  22. }
  23. }
  24.  
  25. unordered_map<ll, frequency> us;
  26. vector<pair<int, frequency>> v;
  27.  
  28. int main() {
  29. // your code goes here
  30. int N, C;
  31. cin>>N>>C;
  32.  
  33. for(int i=0;i<N;i++){
  34. ll value;
  35. cin>>value;
  36.  
  37. if(us.count(value)==0){
  38. frequency f;
  39. f.initial = i;
  40. us.insert(make_pair(value, f));
  41. }else{
  42. us[value].count++;
  43. }
  44. }
  45.  
  46. v.assign(us.begin(), us.end());
  47. sort(v.begin(), v.end(), cmp);
  48.  
  49. for(int i=0;i<v.size();i++){
  50. while(v[i].second.count--){
  51. cout<<v[i].first<<" ";
  52. }
  53. }
  54.  
  55. return 0;
  56. }
Success #stdin #stdout 0s 5292KB
stdin
9 3
1 3 3 3 2 2 2 1 1
stdout
1 1 1 3 3 3 2 2 2