Cod sursa(job #1669887)

Utilizator hegedusPaul Hegedus hegedus Data 31 martie 2016 10:48:33
Problema Elementul majoritar Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 0.62 kb
#include <cstdio>
#include <algorithm>
using namespace std;

const int nmax=1000000;
int v[nmax],n,x,y11,y2,k,mij;

void citire()
{
    freopen("elmaj.in","r",stdin);
    freopen("elmaj.out","w",stdout);
    scanf("%d",&n);
    for(x=1;x<=n;x++)
        scanf("%d",&v[x]);
}

int main()
{
    citire();
    sort(v+1,v+n+1);
    mij=(n+1)/2;
    x=v[mij];
    y11=mij-1;
    y2=mij+1;
    k=1;
    while (x==v[y11])
    {
        y11--;
        k++;
    }

    while (x==v[y2])
    {
        y2++;
        k++;
    }

    if (k>n/2) printf("%d %d\n",x,k);
    else printf("-1\n");
    return 0;
}