Cod sursa(job #2568052)

Utilizator CalinachoGherlan Calin Paul Calinacho Data 3 martie 2020 20:34:17
Problema Elementul majoritar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.92 kb
#include <iostream>
#include <fstream>
using namespace std;
	
///------------------------------------------
	
ifstream in ("elmaj.in");
ofstream out ("elmaj.out");
	
///------------------------------------------ 
	
int n, x, c=1;
int v[1000001];
	
///------------------------------------------
	
int main()
	
{
	
    in >> n;
	
    in >> v[1];
	
    x = v[1];
	
    for(int i = 2; i <= n; i++)
	
    {
	
        in >> v[i];
	
        if(v[i] == x)
	
            c++;
	
        else
	
        {
	
            c--;
	
            if(c == 0)
	
            {
	
                x = v[i];
	
                c = 1;
	
            }
	
        }
	
    }
	
    c = 0;
	
    for(int i = 1; i <= n; i++)
	
    {
	
        if(v[i] == x)
	
            c++;
	
    }
	
    if(c >= n / 2 + 1)
	
        out << x << " " << c;
	
    else
	
        out << "-1";
	
    return 0;
	
}