Mai intai trebuie sa te autentifici.
Cod sursa(job #2438467)
Utilizator | Data | 12 iulie 2019 16:09:35 | |
---|---|---|---|
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");
long long 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;
}