Cod sursa(job #2059929)

Utilizator VanaMarcVana Marc VanaMarc Data 7 noiembrie 2017 19:07:38
Problema Elementul majoritar Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.63 kb
#include <iostream>
#include <fstream>
using namespace std;
int n,v[1000001],ap,cont,x=-1;
int main()
{
    ifstream f("elmaj.in");
    ofstream g("elmaj.out");
    f>>n;
    for (int i=1; i<=n; ++i){
        f>>v[i];
        if (cont==0){
            ++cont;
            x=v[i];
        }
        else{
            if (x==v[i]){
                ++cont;
            }
            else{
                --cont;
            }
        }
    }
    for (int i=1; i<=n; ++i){
        if (x==v[i]){
            ++ap;
        }
    }
    if (ap>n/2){
        g<<x;
    }
    else{
        g<<"0";
    }
    return 0;
}