Cod sursa(job #2438465)
| Utilizator | Data | 12 iulie 2019 16:08:53 | |
|---|---|---|---|
| Problema | Elementul majoritar | Scor | 20 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.66 kb |
# include <iostream>
# include <fstream>
# include <cmath>
using namespace std;
ifstream fi("elmaj.in");
ofstream fo("elmaj.out");
int n, x, elmaj, s;
int main ()
{
fi >> n;
for ( int i = 1; i <= n; ++i) {
fi >> x;
if ( s == 0) {
elmaj = x;
}
else
if ( x == elmaj)
++s;
else
--s;
}
fi.close();
ifstream fi("elmaj.in");
fi >> n;
s = 0;
for ( int i = 1; i <= n; ++i) {
fi >> x;
if ( x == elmaj)
++s;
}
if ( s >= n / 2 + 1)
fo << elmaj << " " << s;
else
fo << -1;
return 0;
}
