Cod sursa(job #1609167)

Utilizator paulstepanovStepanov Paul paulstepanov Data 22 februarie 2016 17:26:44
Problema Elementul majoritar Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.02 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],F[NMax];
int k;

void ReadandSolveandPrint()
{
    fin>>N;
    for(int i = 1; i <= N; ++i)
        {
            int x;
            fin>>x;
            int NotFound = 1;
            for(int j = 1; j <= k; ++j)
                if(V[j] == x)
                    {
                        F[j]++;
                        NotFound = 0;
                    }

            if(NotFound==1)
                {
                    V[++k] = x;
                    F[k] = 1;
                }
        }
    for(int i = 1; i <= k; i++)
        {
            if(F[i] > Max)
                {
                    Max = F[i];
                    Sol = V[i];
                }
        }

    if(Max > (N/2))
        fout<<Sol<<" "<<Max<<"\n";
    else
        fout<<"-1\n";

}



int main()
{
    ReadandSolveandPrint();
    return 0;
}