Cod sursa(job #1691462)

Utilizator AndreiTudorSpiruAndrei Spiru AndreiTudorSpiru Data 18 aprilie 2016 13:58:19
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.45 kb
#include<fstream>
using namespace std;
long long a,x[2000001],max1,nr;
int i,n;
int main ()
{
    ifstream f("elmaj.in");
    ofstream g("elmaj.out");
    f>>n;
    for(i=1;i<=n;i++)
    {
        f>>a;
        x[a]++;
        if(x[a]>max1)
        {
            max1=x[a];
            nr=a;
        }
    }
    if(max1>=n/2+1)
    {
        g<<nr<<" "<<max1;
    }
    else
    {
        g<<"-1";
    }
  f.close();
  g.close();
}