Cod sursa(job #1651926)

Utilizator GinguIonutGinguIonut GinguIonut Data 14 martie 2016 11:11:22
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.84 kb
#include <fstream>
#include <vector>
#define nMax 1000005
#define MOD 66000
#define pb push_back
#define mkp make_pair
using namespace std;
ifstream fin("elmaj.in");
ofstream fout("elmaj.out");
int Sol, n, v[nMax], nrSol;

void read()
{
    fin>>n;
    for(int i=1;i<=n;i++)
        fin>>v[i];
}
void solve()
{
    int cand=-1, k=0;
    for(int i=1;i<=n;i++)
    {
        if(k==0)
        {
            cand=v[i];
            k=1;
        }
        else
        {
            if(cand==v[i])
                k++;
            else
                k--;
        }
    }
    int nr=0;
    for(int i=1;i<=n;i++)
        if(v[i]==cand)
            nr++;
    if(nr>n/2)
        fout<<cand<<" "<<nr;
    else
        fout<<-1;
}
void write()
{
}
int main()
{
    read();
    solve();
    write();
    return 0;
}