Cod sursa(job #1232961)

Utilizator borcanirobertBorcani Robert borcanirobert Data 24 septembrie 2014 13:09:20
Problema Elementul majoritar Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 0.62 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; s = 1;

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

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

        if ( s < 0 )
            nr = a, s = 0;

     //   cout << nr; cin.get();
    }

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