Cod sursa(job #2340235)

Utilizator hhhhhhhAndrei Boaca hhhhhhh Data 10 februarie 2019 09:47:45
Problema Elementul majoritar Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.42 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("elmaj.in");
ofstream fout("elmaj.out");
long long int n,x,i,nr;
int main()
{
    map<int,int>f;
    fin>>n;
    bool ok=0;
    for(i=1;i<=n;i++)
    {
        fin>>x;
        f[x]++;
        if(f[x]>=n/2+1)
        {
            fout<<x<<" "<<f[x];
            ok=1;
            break;
        }
    }
    if(!ok)
        fout<<-1;
    return 0;
}