Cod sursa(job #651448)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 20 decembrie 2011 14:11:54
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
#include <fstream>

#define NMax 1000005

using namespace std;

int N, X[NMax], SX, SN;

void Read ()
{
    ifstream fin ("elmaj.in");
    fin >> N;
    int Who=0, HowMany=0;
    for (int i=1; i<=N; ++i)
    {
        fin >> X[i];
        if (HowMany==0)
        {
            Who=X[i];
        }
        if (Who==X[i])
        {
            ++HowMany;
        }
        if (Who!=X[i])
        {
            --HowMany;
        }
    }
    SX=Who;
    for (int i=1; i<=N; ++i)
    {
        if (X[i]==SX)
        {
            ++SN;
        }
    }
    fin.close ();
}

void Print ()
{
    ofstream fout ("elmaj.out");
    if (SN>=N/2+1)
    {
        fout << SX << " " << SN << "\n";
    }
    else
    {
        fout << "-1\n";
    }
    fout.close ();
}

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