Cod sursa(job #1232959)

Utilizator borcanirobertBorcani Robert borcanirobert Data 24 septembrie 2014 13:01:12
Problema Elementul majoritar Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.57 kb
#include <fstream>
#include <iostream>
using namespace std;

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

long long N;
long long a;
long long nr, s;

int main()
{
    int i;

    fin >> N >> a;
    nr = a;

    for ( i = 2; i <= N; i++ )
    {
        fin >> a;

        if ( a == nr ) s++;
        else           s--;

        if ( s == 0 )
            nr = a;
    }

    if ( s >= 0 )
        fout << nr << ' ' << ( ( N / 2 ) + 1 ) + s << '\n';
    else
        fout << "-1" << '\n';
    fin.close();
    fout.close();
    return 0;
}