Cod sursa(job #2576209)
| Utilizator | Data | 6 martie 2020 17:51:45 | |
|---|---|---|---|
| Problema | Elementul majoritar | Scor | 80 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.67 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream f ("elmaj.in");
ofstream g ("elmaj.out");
int n , x;
int main()
{
int i , cand = -1 , count = 0;
f >> n;
for(i = 1 ; i <= n ; i++)
{
f >> x;
if(cand != x)
{
if(count == 0)
cand = x , count = 1;
else --count;
}
else ++count;
}
ifstream f ("elmaj.in");
f >> n;
count = 0;
for(i = 1 ; i <= n ; i++)
{
f >> x;
if(x == cand)
++count;
}
if(count > n / 2)
g << cand << ' ' << count;
else g << -1;
return 0;
}
