Cod sursa(job #2324635)

Utilizator HerddexJinga Tudor Herddex Data 21 ianuarie 2019 10:42:50
Problema Elementul majoritar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <fstream>
using namespace std;
ifstream fin("elmaj.in");
ofstream fout("elmaj.out");

int main()
{
    int N;
    fin >> N;
    int *A = new int[1000000];

    int candidate = -1;
    int unmatched = 0;
    for (int i = 0; i < N; i++)
    {
        fin >> A[i];
        if(unmatched == 0)
        {
            candidate = A[i];
            unmatched++;
        }
        else
            if(A[i] == candidate)
                unmatched++;
            else
                unmatched--;
    }

    int number_of_votes = 0;
    for (int i = 0; i < N; i++)
    {
        if (A[i] == candidate)
            number_of_votes++;
    }

    if (number_of_votes > N/2)
        fout << candidate << ' ' << number_of_votes;
    else
        fout << -1;

    delete A;
	fin.close();
	fout.close();
	return 0;
}