Cod sursa(job #2553204)

Utilizator BarsanEmilianIoanBarsan Emilian-Ioan BarsanEmilianIoan Data 21 februarie 2020 18:53:51
Problema Elementul majoritar Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb


#include <fstream>









using namespace std;






ifstream cin("elmaj.in");


ofstream cout ("elmaj.out");






int n, x, elmaj, f, s;






int main ()






{
   std::ios::sync_with_stdio(false);
cin.tie();
cout.tie();


   cin >> n;


   cin >> x;


   elmaj = x;


   f = 1;


   for ( int i = 1; i < n; ++i){


       cin >> x;


       if ( f == 0)


           elmaj = x;


       if (elmaj == x)


           ++f;


       else


           --f;


   }


   cin.close();

   cout.close();


   ifstream cin ("elmaj.in");


   ofstream cout ("elmaj.out");
std::ios::sync_with_stdio(false);
cin.tie();
cout.tie();
f = 0;
cin>>n;


   for ( int i = 1; i <= n; ++i){


       cin >> x;


       if(x == elmaj)


           ++f;


   }


   if(f > n/2)


       cout << elmaj << " " << f;


   else


       cout << "-1";


   return 0;


}