Cod sursa(job #1669865)

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

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

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);
    x=v[n/2];
    y1=n/2-1;
    y2=n/2+1;
    k=1;
    do
    {
        if (x==v[y1]) y1--, k++;
        if (x==v[y2]) y2++, k++;
    }while (x==v[y1] || x==v[y2]);
    if (k>n/2) printf("%d %d\n",x,k);
    else printf("-1\n");
    return 0;
}