Cod sursa(job #1052462)

Utilizator PetreFlorinaFMI Petre Florina PetreFlorina Data 11 decembrie 2013 12:43:29
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include <iostream>
#include <fstream>
#include <unordered_map>

using namespace std;

int main()
{
    long n, i, x;
    bool gasit = false;
    //int v[100001];
    unordered_map <int, int> element;
    unordered_map <int, int>::iterator it;

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

    f >> n;
        for (i=1; i<=n; ++i)
            {
                f >> x;
                element[x]++;
            }
    int constanta = n/2+1;

        for ( it=element.begin(); it!=element.end(); ++it)
                if ((it->second >= constanta) && (gasit == false))
                {
                    g << it->first <<" " << it->second;
                    gasit = true;
                }
         if (gasit == false)
                    g << "-1";
    return 0;
}