Cod sursa(job #1609112)

Utilizator paulstepanovStepanov Paul paulstepanov Data 22 februarie 2016 17:01:02
Problema Elementul majoritar Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.65 kb
#include <fstream>
#include <algorithm>
using namespace std;

ifstream  fin("elmaj.in");
ofstream fout("elmaj.out");

const int NMax = 1000005;
int Sol,Max,N,V[NMax];

void Read()
{
    fin>>N;
    for(int i = 1; i <= N; ++i)
        {
            int x;
            fin>>x;
            V[x]++;
        }

}

void SolveandPrint()
{
    for(int i = 1; i<=NMax; i++)
        if(V[i] > Max)
            {
                Max = V[i];
                Sol = i;
            }
    if(Max > (N/2))
        fout<<Sol<<" "<<Max<<"\n";
    else
        fout<<"-1\n";
}

int main()
{
    Read();
    SolveandPrint();
    return 0;
}