Cod sursa(job #2075222)

Utilizator sebi_andrei2008Lazar Eusebiu sebi_andrei2008 Data 25 noiembrie 2017 11:59:16
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.74 kb
#include <iostream>
#include <fstream>
#include <hash_map>
using namespace std;
using namespace __gnu_cxx;

ifstream fin("elmaj.in");
ofstream fout("elmaj.out");

int main()
{
    hash_map<int, int> hashTable;
    hash_map<int, int>::iterator it;
    int x, n;
    fin>>n;
    while (fin>>x) {
        if (hashTable.count(x)) {
            hashTable[x]++;
        } else {
            hashTable[x] = 1;
        }
    }

    bool gasit = 0;
    for (it = hashTable.begin(); it != hashTable.end(); it++) {
        int val = it->second;
        if (val > n/2) {
            fout<<it->first<<' '<<val;
            gasit = 1;
            break;
        }
    }

    if (gasit == 0) {
        fout<<-1;
    }

    return 0;
}