Cod sursa(job #1387538)
| Utilizator | Data | 14 martie 2015 13:30:42 | |
|---|---|---|---|
| Problema | Elementul majoritar | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.59 kb |
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("elmaj.in");
ofstream fout("elmaj.out");
int v[1000001];
int main()
{
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;
}
