Cod sursa(job #2737069)
Utilizator | Moise Alexandru moise_alexandru | Data | 4 aprilie 2021 13:26:26 |
---|---|---|---|
Problema | Elementul majoritar | Scor | 100 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 0.75 kb |
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("elmaj.in");
ofstream out("elmaj.out");
const int maxn = 1000005;
int v[maxn];
int main()
{
int n;
in >> n;
int elmaj = 0;
int cnt = 0;
for(int i = 1; i <= n; i++)
{
int x;
in >> x;
v[i] = x;
if(x == elmaj)
cnt++;
else
{
cnt--;
if(cnt < 0)
{
elmaj = x;
cnt++;
}
}
}
cnt = 0;
for(int i = 1; i <= n; i++)
if(v[i] == elmaj)
cnt++;
if(cnt <= (n / 2))
out << -1;
else
out << elmaj << " " << cnt;
out << "\n";
return 0;
}