Cod sursa(job #1049963)

Utilizator Catalina_BrinzaBrinza Catalina Catalina_Brinza Data 7 decembrie 2013 22:56:06
Problema Elementul majoritar Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1.02 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");


vector <pair <int,int>> hashy[nr_zone];
int cautare (long long val, long long zona)
{
    for (int i=0;i<hashy[zona].size();++i)
    {
        if (hashy[zona][i].first==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].second;
            if (hashy[k][y].second==(n>>1)+1)
            {maxel=x;
                max=hashy[k][y].second; ok=true;
            }}
        else
            hashy[k].push_back(make_pair(x,1));
        }
        else if (x==maxel) ++max;
    }
    if (max==0) g<<maxel;
    else g<<maxel<<' '<<max;
    return 0;
}