Cod sursa(job #2162526)
Utilizator | Data | 12 martie 2018 11:43:56 | |
---|---|---|---|
Problema | Elementul majoritar | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.46 kb |
#include<fstream>
using namespace std;
ifstream f("elmaj.in");
ofstream g("elmaj.out");
int ap[1000001],n,i,M,ok,p;
long long x;
int main ()
{
f>>n;
for(i=1;i<=n;i++)
{
f>>x;
if(x>M)
M=x;
ap[x]++;
}
for(i=1;i<=M&&!ok;i++)
{
if(ap[i]>=n/2+1)
{
p=i;
ok=1;
}
}
if(ok==1)
g<<p<<" "<<ap[p];
else
g<<-1;
return 0;
}