Cod sursa(job #988497)

Utilizator Eugen_VlasieFMI Vlasie Eugen Eugen_Vlasie Data 23 august 2013 00:27:39
Problema Elementul majoritar Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.59 kb
#include <fstream>

using namespace std;
ifstream f("elmaj.in");
ofstream g("elmaj.out");
int n,i,x,k,cand;
long long a[1000001];
int main()
{
    f>>n;
    cand=-1;
    k=0;
    for(i=1;i<=n;i++)
    {
        f>>a[i];
        if(k==0)
        {
            cand=a[i];
            k++;
        }
        else
            if(cand==a[i])
                k++;
            else
                k--;
    }
    k=0;
    for(i=1;i<=n;i++)
        if(cand==a[i])
            k++;
    if(k>n/2)
        g<<cand<<" "<<k<<'\n';
    else
        g<<"-1"<<'\n';
    return 0;
}