Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- using namespace std;
- struct e{
- int val;
- int cnt=0;
- };
- int main()
- {
- int n;
- cin >> n;
- int a[n];
- for(int i=0; i<n; i++)
- cin >> a[i];
- e f[n];
- int elemente_diferite=0;
- for(int i=0; i<n; i++)
- {
- int gasit=0;
- for(int j=0; j<elemente_diferite; j++)
- if (f[j].val==a[i])
- {
- gasit=1;
- f[j].cnt++;
- }
- if (!gasit)
- {
- f[elemente_diferite].val=a[i];
- f[elemente_diferite].cnt++;
- elemente_diferite++;
- }
- }
- int ok=0;
- while (!ok)
- {
- ok=1;
- for(int i=0; i<elemente_diferite-1; i++)
- {
- if (f[i].cnt<f[i+1].cnt)
- {
- int aux=f[i].val;
- f[i].val=f[i+1].val;
- f[i+1].val=aux;
- aux=f[i].cnt;
- f[i].cnt=f[i+1].cnt;
- f[i+1].cnt=aux;
- ok=0;
- }
- }
- }
- a[0]=f[0].val;
- f[0].cnt--;
- ok=0;
- for(int i=1; i<n; i++)
- {
- ok=0;
- for(int j=0; j<elemente_diferite; j++)
- {
- if ((a[i-1]!=f[j].val) && (f[j].cnt>0))
- {
- a[i]=f[j].val;
- f[j].cnt--;
- ok=1;
- break;
- }
- }
- if (!ok) break;
- }
- if (ok)
- {
- for(int i=0; i<n; i++)
- cout << a[i] << " " ;
- }
- else
- cout << "nu exista";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement