Cod sursa(job #2555012)

Utilizator Alin_StanciuStanciu Alin Alin_Stanciu Data 23 februarie 2020 16:44:56
Problema Elementul majoritar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.09 kb
#include <iostream>
#include <fstream>
#include <map>

using namespace std;

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

int n, A[1000001], F[4000000];

int main()
{
    fin >> n;
    int rma = 0, hf = n / 2 + 1;
    bool gasit = false;
    for (int i = 1; i <= n; ++i)
    {
        fin >> A[i];
        int r = A[i] % 4000000;
        ++F[r];
        if (!gasit && F[r] >= hf)
        {
            rma = r;
            gasit = true;
        }
    }
    if (!gasit)
        fout << -1;
    else
    {
        gasit = false;
        int ma;
        for (int i = 0; i < 4000000; ++i)
            F[i] = 0;
        for (int i = 1; i <= n; ++i)
        {
            if (A[i] % 4000000 == rma)
            {
                int c = A[i] / 4000000;
                ++F[c];
                if (!gasit && F[c] >= hf)
                {
                    gasit = true;
                    ma = A[i];
                }
            }
        }
        if (gasit)
            fout << ma << " " << F[ma / 4000000];
        else fout << -1;
    }

    return 0;
}