Cod sursa(job #1239528)

Utilizator FawkesAndrei Colhon Fawkes Data 9 octombrie 2014 10:20:50
Problema Elementul majoritar Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.52 kb
#include <iostream>
#include <fstream>

using namespace std;

int a[86]={0};
int main()
{

    ifstream f("elmaj.in");
    ofstream g("elmaj.out");
    int n,ok=1,mx=0,x;
    f >> n;
    f >> x;
    mx=x;
    for(int i=1;i<=n-1;++i)
    {
        f >> x;
        if(ok==0)
        {
            mx=x;
        }
        if(x==mx)
        {
            ++ok;
        }
        else --ok;
        cout << ok << " ";
    }
    if(ok>=0) g << mx <<" " << (n/2)+(ok+1)/2;
    else g << "-1";
    return 0;
}