Cod sursa(job #639574)

Utilizator idomiralinIdomir Alin idomiralin Data 23 noiembrie 2011 16:34:08
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.54 kb
# include <fstream>

# define inf 0x3f3f3f3

using namespace std;

ifstream f("elmaj.in");
ofstream g("elmaj.out");

int n, ok, aux;
int a[inf], ct[inf];
int main()
{int i;
    
    f>>n;
    for (i = 1; i <= n; i++)
    {
        f>>a[i];
        ct[a[i]]++;          
        }
    
    aux = n / 2 + 1;    
    for (i = 1; i <= n; i++)
    if (ct[a[i]] >= aux)
    {
             g<<a[i]<<" "<<ct[a[i]];
             ct[a[i]] = -10;
             ok = 1;
             }
    if (ok == 0) g<<-1;
    
return 0;
}