Cod sursa(job #1049916)

Utilizator Catalina_BrinzaBrinza Catalina Catalina_Brinza Data 7 decembrie 2013 22:20:05
Problema Elementul majoritar Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.95 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=val%nr_zone;
    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;
    f>>n;
    for (i=0;i<n;++i)
    {
        f>>x;
        y=cautare(x);
        if (y!=-1) {hashy[x%nr_zone][y].sum++; if (hashy[x%nr_zone][y].sum>max) {
            max=hashy[x%nr_zone][y].sum;
            maxel=x;
        }}
        else
        {
            porc q;
            q.v=x;
            q.sum=1;
            hashy[x%nr_zone].push_back(q);
        }
    }
    g<<maxel<<' '<<max;
    return 0;
}