Cod sursa(job #2700764)

Utilizator cyg_mihaizMIHAI ZARAFIU cyg_mihaiz Data 28 ianuarie 2021 18:04:15
Problema Elementul majoritar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include <fstream>

using namespace std;
const int NMAX = 1000000;

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

int v[NMAX + 5];

int main()
{
    ios_base::sync_with_stdio(false);
    fin.tie(NULL);

    int n,i,can,ap;
    fin >> n;
    can = -1;
    ap = 0;
    for(i = 1; i <= n; i++)
    {
        fin >> v[i];
        if(!ap)
            can = v[i], ap = 1;
        else
            if(v[i] == can)
                ap++;
            else
                ap--;
    }
    ap = 0;
    for(i = 1; i <= n; i++)
        if(can == v[i])
            ap++;
    if(ap <= (n >> 1))
        fout << "-1";
    else
        fout << can << " " << ap;
    return 0;
}