Cod sursa(job #1649640)

Utilizator sebinechitasebi nechita sebinechita Data 11 martie 2016 14:27:26
Problema Elementul majoritar Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.71 kb
#include <iostream>
#include <fstream>
using namespace std;
ofstream fout("elmaj.out");


int main()
{
    int n, x, y, i, a;
    ifstream fin("elmaj.in");
    fin >> n;
    x = 0;
    y = 0;
    for(i = 1 ; i <= n ; i++)
    {
        fin >> a;
        if(y == 0)
        {
            x = a;
            y = 1;
        }
        else if(x != a)
        {
            y--;
        }
        else
            y++;
    }
    ifstream fin2("elmaj.in");
    fin2 >> n;
    y = 0;
    for(i = 1 ; i <= n ; i++)
    {
        fin2 >> a;
        if(a == x)
            y++;
    }
    if(y >= n / 2 + 1)
    {
        fout << x << " " << y << "\n";
    }
    else
        fout << "-1\n";
}