Cod sursa(job #1169021)

Utilizator Claudiu95Vartolomei Alexandru Claudiu Claudiu95 Data 10 aprilie 2014 10:33:51
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
#include<fstream>
#define MOD 666013
#include<vector>
#include<algorithm>
using namespace std;
ifstream f("elmaj.in");
ofstream g("elmaj.out");
vector <int> G[MOD+2];
vector<pair<int,int> > P;
int nr,x,n,nrmax,pmax,ok;
int main(){
    f>>n;
    for(int i=1;i<=n;++i){
        f>>x;
        if(x==nrmax){
            ++P[pmax].second;
            continue;
        }
        int poz=x%MOD;
        G[poz].push_back(x);
        if(G[poz].size()>=n/2){
            int okk=1;
            for(int j=0;j<P.size();++j){
                if(P[j].first==x){
                    okk=0;
                    ++P[j].second;
                    if(!nrmax && P[j].second>n/2){
                        pmax=j;
                        nrmax=x;
                        ok=1;
                    }
                }
            }
            if(okk){
                P.push_back(make_pair(x,0));
                int nr=0;
                for(int i=0;i<G[poz].size();++i)
                    if(G[poz][i]==x)
                        ++nr;
                P[P.size()-1].second=nr;
                }
        }
    }
    if(ok)
        g<<nrmax<<" "<<P[pmax].second;
    else
        g<<-1;
    return 0;
}