Cod sursa(job #2311320)

Utilizator NToniBoSSNicolae Tonitza NToniBoSS Data 2 ianuarie 2019 21:51:19
Problema Elementul majoritar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.14 kb
#include <bits/stdc++.h>
#define LIM 1<<17
/// TONI BO$$ was here
/// #MLC

using namespace std;
char BUF[LIM];
int poz;

inline char getChar(){
    poz++;
    if(poz>=LIM){
    	fread(BUF,LIM,1,stdin);
    	poz=0;
    }
    return BUF[poz];
}

inline int getNr(){
    int r=0, semn=1;
    char ch=getChar();
    while(isdigit(ch)==0 && ch!='-') ch=getChar();
    if(ch=='-'){
        semn=-1;
        ch=getChar();
    }
    while(isdigit(ch)!=0){
        r=r*10+semn*(ch-'0');
        ch=getChar();
    }
    return r;
}

int v[1000001];

int main()
{
    int n,i,ap,el;
    freopen("elmaj.in","r",stdin);
    freopen("elmaj.out","w",stdout);
    n=getNr();
    for(i=1; i<=n; i++)
        v[i]=getNr();
    ap=1;
    el=v[1];
    for(i=2; i<=n; i++)
    {
        if(v[i]!=el)
            ap--;
        else
            ap++;
        if(ap<0)
        {
            el=v[i];
            ap=1;
        }
    }
    ap=0;
    for(i=1; i<=n; i++)
        if(el==v[i])
            ap++;
    if(ap<(n/2)+1)
        el=-1,ap=0;
    printf("%d",el);
    if(ap>0)
        printf(" %d",ap);

    return 0;
}