Cod sursa(job #2795903)

Utilizator andrei_marciucMarciuc Andrei andrei_marciuc Data 7 noiembrie 2021 10:47:16
Problema Elementul majoritar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.98 kb
#pragma GCC optimize("O3")
#include <stdio.h>
#include <unistd.h>
 
#define CHUNK ( 1 << 24 )
#define NMAX 1000000
 
static char buf[ CHUNK ];
static size_t bpos, v[ NMAX ];
 
static inline size_t read_int( void ) {
    size_t x = 0;
 
    while( '0' <= buf[ bpos ] ) {
        x = x * 10 + ( buf[ bpos ] - '0' );
        bpos++;
    }
 
    bpos++;
    return x;
}

int n;
int m, k;

int main()
{
    freopen( "elmaj.in", "r", stdin );
    read( STDIN_FILENO, buf, CHUNK );
 
    n = read_int();
    
    int *v = new int[ n ];
    for( int i = 0; i < n; i++ ) {
        v[ i ] = read_int();
        if( k == 0 ) {
            m = v[ i ];
            k = 1;
        } else if( v[ i ] != m )
            --k;
        else ++k;
    }
    fclose( stdin );

    k = 0;
    for( int i = 0; i < n; i++ )
        k += ( v[ i ] == m );

    FILE *fout = fopen( "elmaj.out", "w" );
    if( k > n / 2 )
        fprintf( fout, "%d %d\n", m, k );
    else fprintf( fout, "-1\n" );
    fclose( fout );
    return 0;
}