Cod sursa(job #1410031)

Utilizator pulseOvidiu Giorgi pulse Data 30 martie 2015 20:18:05
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.57 kb
#include <fstream>

using namespace std;

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

const int maxn = 1000010;

int n, a[maxn], cnt, x;

int main()
{
    fin >> n;
    for (int i = 1; i <= n; i++)
        fin >> a[i];
    for (int i = 1; i <= n; i++)
    {
        if (cnt == 0)
        {
            x = a[i];
            cnt++;
        }
        else if (a[i] == x) cnt++;
        else cnt--;
    }
    cnt = 0;
    for (int i = 1; i <= n; i++)
        if (a[i] == x) cnt++;
    if (cnt >= n / 2 + 1)
        fout << x << " " << cnt;
    else fout << -1;
    return 0;
}