Cod sursa(job #2389172)

Utilizator petrisorvmyVamanu Petru Gabriel petrisorvmy Data 26 martie 2019 20:58:27
Problema Elementul majoritar Scor 100
Compilator cpp-64 Status done
Runda excelenta-tema3 Marime 0.52 kb
#include <fstream>
#include <unordered_map>

using namespace std;
ifstream f("elmaj.in");
ofstream g("elmaj.out");

int n,x,minim, ans;
unordered_map <int, int> mymap;
int main()
{
    f >> n;
    minim = (n/2) + 1;
    for(int i = 1; i <= n; ++ i)
    {
        f >> x;
        mymap[x]++;
        if(mymap[x] >= minim && ans == 0)
        {
            ans = x;
        }
    }
    if(!ans)
        g << -1;
    else
        g << ans << ' ' << mymap[ans];
    f.close(); g.close();
    return 0;
}