Cod sursa(job #3131486)

Utilizator Traian_7109Traian Mihai Danciu Traian_7109 Data 20 mai 2023 13:30:04
Problema Elementul majoritar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.65 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int NMAX = 1e6;
int a[NMAX+5];

int main()
{
    int n, current_candidate, number_of_votes = 0, total_number = 0;
    fin>>n;

    for (int i = 1; i <= n; i++) {
        fin>>a[i];

        if (number_of_votes == 0) current_candidate = a[i];

        if (a[i] == current_candidate) number_of_votes++;
        else number_of_votes--;
    }

    for (int i = 1; i <= n; i++)
        if (a[i] == current_candidate) total_number++;
    
    if (total_number > n/2) fout<<current_candidate<<' '<<total_number;
    else fout<<-1;

    return 0;
}