Cod sursa(job #1418658)

Utilizator mihai.constantinConstantin Mihai mihai.constantin Data 13 aprilie 2015 16:56:35
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 kb
#include <iostream>
#include <cstdio>
#define dmax 1000010
using namespace std;

long N; long v[dmax];

int main()
{
    freopen("elmaj.in", "r", stdin);
    freopen("elmaj.out", "w", stdout);

    long i;

    scanf("%ld",&N);
    for(i=1; i<=N; i++) scanf("%ld",&v[i]);

    long cand=-1,k=0;

    for(i=1; i<=N; i++)
    {
        if(k==0) {cand=v[i]; k=1;}
        else
        {   if(v[i]==cand) k++; // NU AM PUTUT IMPERECHEA PE VOTANTUL i SI ASTFEL TREBUIE SA MARIM NUMARUL DE VOTANTI NECUPLATI
            else
                k--; //CUPLAM VOTANTUL i CU ORICE VOTANT CE IL SUSTINE PE cand SI MISCORAM NUMERUL DE VOTANTI NECUPLATI
        }
    }

    long NR=0;
    for(i=1; i<=N; i++)
        if(v[i]==cand) NR++;

    if(NR > N/2) printf("%ld %ld",cand,NR);
    else printf("-1");

    return 0;
}