Cod sursa(job #2389168)

Utilizator petrisorvmyVamanu Petru Gabriel petrisorvmy Data 26 martie 2019 20:55:34
Problema Elementul majoritar Scor 90
Compilator cpp-64 Status done
Runda excelenta-tema3 Marime 0.53 kb
#include <fstream>
#include <map>

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

int n,x,minim, ans;
map <int, int> mymap;
map <int, int> :: iterator it;
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;
}