Cod sursa(job #1387536)
Utilizator | Preda Andrei preda.andrei | Data | 14 martie 2015 13:28:11 |
---|---|---|---|
Problema | Elementul majoritar | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.6 kb |
#include <iostream>
#include <fstream>
using namespace std;
int v[1000001];
int main()
{
ifstream fin("elmaj.in");
ofstream fout("elmaj.out");
int n, x, val=-1, con=1;
fin >> n;
for(int i=1; i<=n; i++){
fin >> v[i];
if(v[i]==val)
con++;
else
con--;
if(con==0){
val=v[i];
con=1;
}
}
con=0;
for(int i=1; i<=n; i++)
if(v[i]==val)
con++;
if(con>=(n/2)+1)
fout << val << " " << con;
else
fout << -1;
return 0;
}