Mai intai trebuie sa te autentifici.
Cod sursa(job #2555011)
| Utilizator | Data | 23 februarie 2020 16:43:59 | |
|---|---|---|---|
| Problema | Elementul majoritar | Scor | 50 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 1.09 kb |
#include <iostream>
#include <fstream>
#include <map>
using namespace std;
ifstream fin("elmaj.in");
ofstream fout("elmaj.out");
int n, A[1000001], F[5000000];
int main()
{
fin >> n;
int rma = 0, hf = n / 2 + 1;
bool gasit = false;
for (int i = 1; i <= n; ++i)
{
fin >> A[i];
int r = A[i] % 5000000;
++F[r];
if (!gasit && F[r] >= hf)
{
rma = r;
gasit = true;
}
}
if (!gasit)
fout << -1;
else
{
gasit = false;
int ma;
for (int i = 0; i < 5000000; ++i)
F[i] = 0;
for (int i = 1; i <= n; ++i)
{
if (A[i] % 5000000 == rma)
{
int c = A[i] / 5000000;
++F[c];
if (!gasit && F[c] >= hf)
{
gasit = true;
ma = A[i];
}
}
}
if (gasit)
fout << ma << " " << F[ma / 5000000];
else fout << -1;
}
return 0;
}
