Cod sursa(job #2059932)
Utilizator | Vana Marc VanaMarc | Data | 7 noiembrie 2017 19:08:27 |
---|---|---|---|
Problema | Elementul majoritar | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.64 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<<" "<<ap;
}
else{
g<<"-1";
}
return 0;
}