Cod sursa(job #1367033)

Utilizator viuscenkoViktor Iuscenko viuscenko Data 1 martie 2015 16:01:51
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.34 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

#define MAXN 1000010

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

int n, elmaj, elcount;
int a[MAXN];
fpos_t pos;

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

    fin >> n;

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

    elcount = 0;
    for(int i = 0; i < n; ++i) {
        if(a[i] == elmaj)
            elcount++;
    }
    if(elcount > (n >> 1))
        fout << elmaj << " " << elcount;
    else
        fout << -1;
    return 0;
}