Cod sursa(job #656537)

Utilizator rootsroots1 roots Data 4 ianuarie 2012 19:19:44
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.72 kb
#include <fstream>

#define vL 1000001

using namespace std;

ifstream in;
ofstream out;

int v[vL];

int main()
{
    int N;

    in.open("elmaj.in");

    in>>N;
    for(int i=1;i<=N;++i) in>>v[i];

    in.close();

    int vot=-1;
    int cnt=0;

    for(int i=1;i<=N;++i)
        if(cnt==0)
        {
            vot=v[i];
            cnt=1;
        }
        else
        if(v[i]!=vot) --cnt;
        else ++cnt;

    out.open("elmaj.out");

    if(cnt<0) out<<"-1\n";
    else
    {
        cnt=0;
        for(int i=1;i<=N;++i)
            if(v[i]==vot) ++cnt;

        if(cnt>(N>>1)) out<<vot<<' '<<cnt<<'\n';
        else out<<"-1\n";
    }

    out.close();

    return 0;
}