Cod sursa(job #1049948)

Utilizator Catalina_BrinzaBrinza Catalina Catalina_Brinza Data 7 decembrie 2013 22:47:24
Problema Elementul majoritar Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
//
//  main.cpp
//  elmaj
//
//  Created by Catalina Brinza on 12/7/13.
//  Copyright (c) 2013 Catalina Brinza. All rights reserved.
//

#include <fstream>
#include <vector>
#define nr_zone 666013
using namespace std;
ifstream f("elmaj.in");
ofstream g("elmaj.out");


struct porc
{
    long long v,sum;
};

vector <porc> hashy[nr_zone];
int cautare (long long val, long long zona)
{
    for (int i=0;i<hashy[zona].size();++i)
    {
        if (hashy[zona][i].v==val) return i;
    }
    return -1;
}

int main()
{long long n,i,x,y,max=0,maxel=-1;
    f>>n;
    bool ok=false;
    for (i=0;i<n;++i)
    {
        f>>x;
        if (!ok)
        {
        long long k=x%nr_zone;
        y=cautare(x,k);
        if (y!=-1) {
            ++hashy[k][y].sum;
            if (hashy[k][y].sum==n/2+1)
            {maxel=x;
                max=hashy[k][y].sum; ok=true;
            }}
        else
        {
            porc q;
            q.v=x;
            q.sum=1;
            hashy[k].push_back(q);
        }}
        else if (x==maxel) ++max;
    }
    if (max==0) g<<maxel;
    else g<<maxel<<' '<<max;
    return 0;
}