Cod sursa(job #1367027)

Utilizator viuscenkoViktor Iuscenko viuscenko Data 1 martie 2015 15:59:37
Problema Elementul majoritar Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.35 kb
#include <bits/stdc++.h>

using namespace std;

#define     mp              make_pair
#define     fs              first
#define     sc              second
#define     pob             pop_back
#define     pub             push_back
#define     eps             1E-7
#define     sz(a)           a.size()
#define     count_one       __builtin_popcount;
#define     count_onell     __builtin_popcountll;
#define     fastIO          ios_base::sync_with_stdio(false)
#define     PI              (acos(-1.0))
#define     linf            (1LL<<62)//>4e18
#define     inf             (0x7f7f7f7f)//>2e9

#define DEBUG 1
#ifdef DEBUG
#define D(x) x
#else
#define D(x)
#endif

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

int n, elmaj, elcount;
fpos_t pos;

int main()
{
    int x;
    elcount = 1;

    fin >> n;

    fin >> elmaj;
    for(int i = 1; i < n; ++i) {
        fin >> x;
        if(x == elmaj)
            elcount++;
        else if(elcount > 0)
            elcount--;
        else
            elmaj = x, elcount = 1;
    }

    fin.close();
    fin.open("elmaj.in"); fin >> n;
    elcount = 0;
    for(int i = 0; i < n; ++i) {
        fin >> x;
        if(x == elmaj)
            elcount++;
    }
    if(elcount > (n >> 1))
        fout << elmaj << " " << elcount;
    else
        fout << -1;
    return 0;
}