Cod sursa(job #2124579)

Utilizator AlexandruGabrielAliciuc Alexandru AlexandruGabriel Data 7 februarie 2018 13:10:21
Problema Elementul majoritar Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 0.56 kb
#include <bits/stdc++.h>

using namespace std;

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

unsigned long long a[1000001],cand=-1;

int main()
{
    int n,cnt=0,k=0;
    fin>>n;
    for (int i=1;i<=n;i++)
        fin>>a[i];
    for (int i=1;i<=n;i++)
    {
        if (k==0)
        {
            cand=a[i];
        }
        else if (a[i]==cand) k++;
        else k--;
    }
    for (int i=1;i<=n;i++)
        if (a[i]==cand) cnt++;
    if (cnt>n/2)
    {
        fout<<cand<<" "<<cnt;
    }
    else fout<<-1;
    return 0;
}