Advertisement
CosminVarlan

Infoiasi 2014 II 4

Jul 19th, 2018
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3.  
  4. using namespace std;
  5.  
  6. struct e{
  7.     int val;
  8.     int cnt=0;
  9. };
  10.  
  11.  
  12. int main()
  13. {
  14.     int n;
  15.     cin >> n;
  16.     int a[n];
  17.     for(int i=0; i<n; i++)
  18.         cin >> a[i];
  19.     e f[n];
  20.     int elemente_diferite=0;
  21.  
  22.     for(int i=0; i<n; i++)
  23.     {
  24.         int gasit=0;
  25.         for(int j=0; j<elemente_diferite; j++)
  26.             if (f[j].val==a[i])
  27.             {
  28.                 gasit=1;
  29.                 f[j].cnt++;
  30.             }
  31.         if (!gasit)
  32.         {
  33.             f[elemente_diferite].val=a[i];
  34.             f[elemente_diferite].cnt++;
  35.             elemente_diferite++;
  36.         }
  37.     }
  38.  
  39.     int ok=0;
  40.     while (!ok)
  41.     {
  42.         ok=1;
  43.         for(int i=0; i<elemente_diferite-1; i++)
  44.         {
  45.             if (f[i].cnt<f[i+1].cnt)
  46.             {
  47.                 int aux=f[i].val;
  48.                 f[i].val=f[i+1].val;
  49.                 f[i+1].val=aux;
  50.  
  51.                 aux=f[i].cnt;
  52.                 f[i].cnt=f[i+1].cnt;
  53.                 f[i+1].cnt=aux;
  54.  
  55.                 ok=0;
  56.             }
  57.         }
  58.     }
  59.  
  60.     a[0]=f[0].val;
  61.     f[0].cnt--;
  62.  
  63.     ok=0;
  64.     for(int i=1; i<n; i++)
  65.     {
  66.         ok=0;
  67.         for(int j=0; j<elemente_diferite; j++)
  68.         {
  69.             if ((a[i-1]!=f[j].val) && (f[j].cnt>0))
  70.             {
  71.                 a[i]=f[j].val;
  72.                 f[j].cnt--;
  73.                 ok=1;
  74.                 break;
  75.             }
  76.         }
  77.         if (!ok) break;
  78.     }
  79.  
  80.     if (ok)
  81.     {
  82.         for(int i=0; i<n; i++)
  83.             cout << a[i] << " " ;
  84.     }
  85.     else
  86.         cout << "nu exista";
  87.  
  88.  
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement