Cod sursa(job #2582546)

Utilizator PetrescuAlexandru Petrescu Petrescu Data 16 martie 2020 21:24:14
Problema Elementul majoritar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include <bits/stdc++.h>
#define MAX 1000000 + 5

using namespace std;

int v[MAX];
int main()
{
    ifstream fin("elmaj.in");
    ofstream fout("elmaj.out");

    ios::sync_with_stdio(false);
    fin.tie(0);
    fout.tie(0);
    srand(time(NULL));

    int n, apMax = 0, elMax = -1;

    fin >> n;

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

        if(v[i] != elMax)
        {
            if(apMax == 0)
            {
                elMax = v[i];
                apMax = 1;
            }
            else apMax--;
        }
        else apMax++;
    }

    apMax = 0;

    for(int i = 1; i <= n; i++)
        if(v[i] == elMax)apMax++;

    if(apMax > n / 2)fout << elMax << " " << apMax;
    else fout << -1;

    fin.close();
    fout.close();

    return 0;
}