Cod sursa(job #2802244)

Utilizator andreiiorgulescuandrei iorgulescu andreiiorgulescu Data 17 noiembrie 2021 20:40:02
Problema Elementul majoritar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
#include <bits/stdc++.h>

using namespace std;

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

int tip,nrtip,ap;
int n,a[1000005];

int main()
{
    in >> n;
    for (int i = 1; i <= n; i++)
    {
        in >> a[i];
        if (nrtip == 0)
        {
            tip = a[i];
            nrtip = 1;
        }
        else
        {
            if (a[i] == tip)
                nrtip++;
            else
                nrtip--;
        }
        //cout << a[i] << " " << tip << " " << nrtip << '\n';
    }
    for (int i = 1; i <= n; i++)
        if (a[i] == tip)
            ap++;
    if (ap >= n / 2 + 1)
        out << tip << " " << ap;
    else
        out << -1;
    return 0;
}