Cod sursa(job #1398192)

Utilizator zpaePopescu Andreea zpae Data 24 martie 2015 01:12:29
Problema Elementul majoritar Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.49 kb
#include <cstdio>
#include <map>
using namespace std;

int main()
{

    ifstream in ("elmaj.in");
    ofstream out ("elmaj.out");
    int n,i,m=0,x;
    map<int,int> f;
    f[0]=-1;
    in>>n;
    for(i=0;i<n;i++)
    {
        in>>x;
        f[x]++;
        if(f[x]>f[m])
        {
            m=x;
            if(f[m]>n/2)
            {
                out<<m<<' '<<f[m]<<"\n";
                return 0;
            }
        }
    }
    out<<"-1\n";
    return 0;
}