Cod sursa(job #1348608)

Utilizator Burbon13Burbon13 Burbon13 Data 19 februarie 2015 19:43:14
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
#include <iostream>
#include <cstdio>

#define n_max 1000005

using namespace std;

int v[n_max] , n ;

void citire()
{
    scanf( "%d" , &n ) ;
    for ( int i = 1 ; i <= n ; i ++ )
        scanf( "%d" , &v[i] ) ;
}

void el_maj()
{
    int cand = -1 , k = 0 ;
    for ( int i = 1 ; i <= n ; i ++ )
        if ( k == 0 )
        {
            cand = v[i] ;
            k = 1 ;
        }
        else if ( cand == v[i] )
            k ++ ;
        else
            k -- ;
    int sum = 0 ;
    for ( int i = 1 ; i <= n ; i ++ )
        if ( v[i] == cand )
            sum ++ ;
    if ( sum > n / 2  )
        printf( "%d %d\n" , cand , sum ) ;
    else
        printf( "-1\n" ) ;
}

int main()
{
    freopen( "elmaj.in" , "r" , stdin ) ;
    freopen( "elmaj.out" , "w" , stdout ) ;
    citire() ;
    el_maj() ;
    return 0;
}