Cod sursa(job #1317612)

Utilizator thesvcoolmanLucian Bicsi thesvcoolman Data 15 ianuarie 2015 00:13:51
Problema Elementul majoritar Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 0.86 kb
#include<fstream>
#include<vector>
#include<utility>
#define MOD 1000000

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

vector<pair<long, int> > H[MOD];
vector<pair<long, int> >::iterator it;
int n;


int add(long x) {
    for(it=H[x%MOD].begin(); it!=H[x%MOD].end(); ++it) {
        if((*it).first == x) {
            (*it).second++;
            if((*it).second >= n/2+1) {
                return (*it).second;
            }
            return -1;
        }
    }
    H[x%MOD].push_back(make_pair(x, 1));
    return -1;
}

int main() {
    fin>>n;
    long maj, x, res = 0, nr;
    for(int i=1; i<=n; i++) {
        fin>>x;
        res = add(x);
        if(res != -1) {
            maj = x;
            nr = res;
        }
    }
    if(!res) fout<<-1;
    else
    fout<<maj<<" "<<res;
    return 0;
}