Cod sursa(job #1284119)

Utilizator TodeTodeAlexandru Toderica TodeTode Data 6 decembrie 2014 11:15:39
Problema Elementul majoritar Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.58 kb
#include <fstream>
#include <algorithm>

using namespace std;

int a[1000005];

int main()
{
    int n, i, cnt, mx = 1, elmaj;
    ifstream fin("elmaj.in");
    fin >> n;
    for(i = 1; i <= n; i++) fin >> a[i];
    sort(a + 1, a + n + 1);
    for(i = 2; i <= n; i++)
    {
        if(a[i] == a[i - 1]) cnt++;
        else cnt = 1;
        if(mx < cnt)
        {
            mx = cnt;
            elmaj = a[i];
        }
    }
    ofstream fout("elmaj.out");
    if(mx < n / 2 + 1) fout << "-1";
    else fout << elmaj << " " << mx;
    fout.close();
    return 0;
}