Cod sursa(job #1064600)

Utilizator mariacMaria Constantin mariac Data 22 decembrie 2013 01:51:04
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 kb
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("elmaj.in");
ofstream fout("elmaj.out");
struct per
{
    int val, nr_ap;
};
vector<per> hashh[666013];
int max_ap, elmaj;
int N;
int main()
{
    int i, x, j;
    fin >> N;
    for( i = 1; i <= N; i++)
    {
        fin >> x;
        int ok = 0, key = x % 666013;
        for(j = 0; j < hashh[key].size() && !ok; j++)
            if(hashh[key][j].val == x)
            {
                ok = 1;
                hashh[key][j].nr_ap ++;
                if( hashh[key][j].nr_ap > max_ap)
                {
                    max_ap =  hashh[key][j].nr_ap;
                    elmaj = x;
                }
            }
        if(! ok)
        {
            per aux;
            aux.val = x;
            aux.nr_ap = 1;
            if(max_ap == 0)
            {
                max_ap = 1;
                elmaj = x;
            }
            hashh[key].push_back(aux);
        }
    }
    if(max_ap >= N / 2 + 1)fout<<elmaj <<" "<<max_ap;
    else fout<<-1;
    return 0;
}