Cod sursa(job #2216537)
| Utilizator | Data | 27 iunie 2018 10:28:06 | |
|---|---|---|---|
| Problema | Elementul majoritar | Scor | 80 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.54 kb |
#include <fstream>
using namespace std;
ifstream fin ("elmaj.in");
ofstream fout ("elmaj.out");
const int Dim = 1000001;
int cnt,elmaj,x,n;
int main() {
fin >> n;
for ( int i = 1; i <= n; ++i) {
fin >> x;
if ( cnt == 0) {
elmaj = x;
cnt = 1;
}
else
{ if ( x == elmaj)
++cnt;
else
--cnt;
}
}
fin.close();
ifstream fin ("elmaj.in");
fin >> n;
cnt = 0;
for ( int i = 1; i <= n; ++i) {
fin >> x;
if ( x == elmaj)
++cnt;
}
if ( cnt > n / 2)
fout << elmaj << " " << cnt;
else
fout << -1;
}
