Cod sursa(job #2252831)

Utilizator alexandrucuravalealexandru curavale alexandrucuravale Data 3 octombrie 2018 09:44:24
Problema Elementul majoritar Scor 0
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2013 Marime 0.47 kb
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("elmaj.in");
ofstream g("elmaj.out");
double v[1000001];
int n,nr,mx,p;
int main()
{f>>n;
for(int i=1;i<=n;i++)
{
    f>>v[i];
}
sort(v+1,v+n+1);
nr=1;
for(int i=2;i<=n;i++)
{
    if(v[i]==v[i-1]){nr++;}
    else
       {

        if(nr>mx)
        {mx=nr;p=v[i-1];}
        nr=1;
        }

}
if(nr>mx)mx=nr;p=v[n];
if(mx>=n/2+1){g<<p<<" "<<mx;}
else{g<<-1;}

    return 0;
}