Cod sursa(job #664201)

Utilizator blasterzMircea Dima blasterz Data 19 ianuarie 2012 20:00:08
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.99 kb
#include <cstdio>
#include <algorithm>

using namespace std;

#define dim 8192
char ax[dim];
int pz;
inline void cit(int &x) {
    x = 0;
    while (ax[pz] < '0' || ax[pz] > '9')
        if (++pz == dim)
            fread (ax, 1, dim, stdin), pz = 0;
    while (ax[pz] >= '0' && ax[pz] <= '9') {
        x = x * 10 + ax[pz] - '0';
        if (++pz == dim)
            fread (ax, 1, dim, stdin), pz = 0;
    }
}
int a[1000001];
int n;
int main () {
    freopen ("elmaj.in", "r", stdin);
    freopen ("elmaj.out", "w", stdout);
    cit (n);
    int i;
    for (i = 1; i <= n; ++i)
        cit (a[i]);
    sort (a + 1, a + n + 1);

    int nr = 1, v, nrmax = 0;
    for (i = 2; i <= n; ++i)
        if (a[i] == a[i - 1])
            ++nr;
        else {
            if (nr > nrmax)
                nrmax = nr, v = a[i - 1];
            nr = 1;
        }
    if (nr > nrmax)
        nrmax = nr, v = a[n];
    if (nrmax >= 1 + n/2)
        printf ("%d %d\n", v, nrmax);
    else
        printf ("-1\n");

    return 0;
}