Cod sursa(job #1233296)

Utilizator Mitsa3Neamt Mihai Mitsa3 Data 25 septembrie 2014 08:40:29
Problema Elementul majoritar Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.69 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("elmaj.in");
ofstream fout("elmaj.out");
#define MAX 1000005
int a[MAX];
int main()
{
    int n;
    fin >> n;
    for(int i = 1; i<=n; i++)
        fin >> a[i];
    sort(a+1,a+n+1);
    int r = n/2 +1;
    int z = 1;
    bool ok = 1;
    for(int i = 1; i<n; i++)
    {
        if(a[i] == a[i+1])
            z++;
        else
        {
            if(z == r)
                {fout << a[i] << " " << z << "\n";ok=0;}
            z = 1;
        }
    }
    if(z == r)
        {fout << a[n] << " " << z << "\n";ok=0;}
    if(ok==1)
        fout << -1 << "\n";
    return 0;
}