Cod sursa(job #1053253)

Utilizator Catalina_BrinzaBrinza Catalina Catalina_Brinza Data 12 decembrie 2013 16:31:03
Problema Elementul majoritar Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.99 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 main()
{long long n,i,x,y,max=0,maxel=-1,ji;
    f>>n;
    bool ok=false;
    for (i=0;i<n;++i)
    {
        f>>x;
        if (!ok)
        {
        long long k=x%nr_zone;
            bool ok2=false;
            for (ji=0;ji<hashy[k].size();++ji)
            {
                if (hashy[k][ji].first==x) {ok2=true; ++hashy[k][ji].second; if (hashy[k][ji].second==(n>>1)+1)
                {maxel=x;
                    max=hashy[k][ji].second; ok=true;
                }break;}
            }
        
        if (!ok)
            hashy[k].push_back(make_pair(x,1));
        }
        else if (x==maxel) ++max;
    }
    
    if (max==0) g<<maxel;
    else g<<maxel<<' '<<max;
    return 0;
}