Cod sursa(job #1987870)

Utilizator ArctopusKacso Peter-Gabor Arctopus Data 1 iunie 2017 12:53:58
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.93 kb
#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
#include <queue>
#include <climits>
#include <map>

#define ll long long
#define pb push_back

using namespace std;

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

const int NLIM = 1e6 + 10;

int  N;
int v[NLIM];

int main()
{
    ios::sync_with_stdio( false );

    fin >> N;
    int k = 0;
    int res = -1;
    for( int i = 0; i < N; ++i )
    {
        fin >> v[i];
        if( k == 0 )
        {
            k = 1;
            res = v[i];
        }
        else if( v[i] == res )
            ++k;
        else
            --k;
    }

    if( res < 0 )
    {
        fout << -1;
        return 0;
    }

    int nr = 0;
    for( int i = 0; i < N; ++i )
    {
        if( v[i] == res )
            ++nr;
    }

    if( nr >= ( N / 2 ) + 1 )
        fout << res << " " << nr << "\n";


    return 0;
}