Advertisement
mbah_bejo

ALAN SURYAJANA TOURING

May 6th, 2020
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef struct petaKu
  5. {
  6.     int position;
  7.     int frequency,num;
  8. }petaKu;
  9.  
  10. void urut( int awal, int akhir, petaKu sb[], int kode)
  11. {
  12. //printf("%d%d\n",awal,akhir);
  13.     petaKu temp;
  14.     int i, mot= akhir;
  15.     for(i=awal;i<mot;i++)
  16.     {
  17.     if (mot<0) break;
  18.         if((kode==1 && sb[i].frequency<sb[mot].frequency)
  19.         ||(kode==2 && sb[i].position>sb[mot].position && sb[i].frequency==sb[mot].frequency))
  20.         {
  21.             temp=sb[i];
  22.  
  23.             sb[i]=sb[mot-1];
  24.             sb[mot-1]=sb[mot];
  25.             sb[mot]=temp;
  26.             i--; mot--;
  27.             //printf("%d%d\n",i,mot);
  28.         }
  29.     }
  30. //  for(i=0;i<akhir;i++)
  31. //  printf("%s\n",sb[i].nama);
  32.     if (mot-1-awal > 0) urut(awal,mot-1,sb,kode);
  33.     if( akhir - (mot+1) > 0) urut(mot+1,akhir, sb,kode);
  34. }
  35.  
  36. int main()
  37. {
  38.     ios::sync_with_stdio(false);
  39.     int n,k,i,t;
  40.     cin >> n >> k;
  41.     i=n;
  42.     map<int,int> mapKu_freg;
  43.     map<int,int> mapKu_pos;
  44. /*
  45. 3 6 6 6 4 4 4 3 3
  46.  
  47. freg[3] = 1
  48. pos[0] = 3
  49.  
  50. freg[6] = 3
  51. pos[1] = 6
  52.  
  53. freg[4] = 3
  54. pos[2] = 4
  55. */
  56.     int posisi=0;
  57.     while(i--)
  58.     {
  59.             cin >> t;
  60.  
  61.             if(mapKu_freg.count(t)==0)
  62.             {
  63.                 mapKu_freg.insert({t,1});
  64.                 mapKu_pos.insert({posisi,t});
  65.                 posisi++;
  66.             }else{
  67.             mapKu_freg[t]++;
  68.             }
  69.     }
  70.     petaKu temp[posisi];
  71.  
  72. /*
  73. 3 6 6 6 4 4 4 3 3
  74.  
  75. position 0
  76. temp[0].num = 3
  77. temp[0].frecun = 3
  78.  
  79.  
  80. */
  81.  
  82.     for(int j= 0;j<posisi;j++)
  83.     {
  84.         temp[j].position= j;
  85.         temp[j].num = mapKu_pos[j];
  86.         temp[j].frequency = mapKu_freg[temp[j].num];
  87.     }
  88.     urut(0,posisi-1,temp, 1);
  89.     urut(0,posisi-1,temp, 2);
  90.  
  91.     for(int j= 0;j<posisi;j++)
  92.     {
  93.         for(int k = 0; k < temp[j].frequency;k++)
  94.  
  95.         {cout << temp[j].num << ' ' ;}
  96.  
  97.     }
  98.     cout << endl ;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement