Cod sursa(job #1778668)

Utilizator elffikkVasile Ermicioi elffikk Data 13 octombrie 2016 23:05:03
Problema Elementul majoritar Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 0.55 kb
#include <iostream>
#include <fstream>
#include <map>
using namespace std;

main() {
    ifstream cin("elmaj.in");
    ofstream cout("elmaj.out");
    int n, maj = -1;
    cin>>n;
    map <int, int> h;
    for (int i = 0; i < n; i++) {
        int x;
        cin>>x;
        if (h.find(x) == h.end()) {
            h[x] = 1;
        } else {
            h[x] = h[x] + 1;
        }
        if (h[x] > n/2) {
            maj = x;
        }
    }
    if (maj > -1) {
        cout<<maj<<" "<<h[maj];
    } else {
        cout<<-1;
    }
}