Cod sursa(job #3147744)

Utilizator amcbnCiobanu Andrei Mihai amcbn Data 26 august 2023 21:16:38
Problema Elementul majoritar Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.07 kb
#include <bits/stdc++.h>
using namespace std;
string out[2]{ "NO", "YES" };
ifstream fin("elmaj.in");
ofstream fout("elmaj.out");
const char sp = ' ';
const char nl = '\n';
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
#define all(y) y.begin(), y.end()

int n;
const int sqvalmax = 44722;
int a[sqvalmax + 5]{ 0 }, b[sqvalmax + 5]{ 0 };

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    fin >> n;
    fill(a, a + sqvalmax + 1, 0);
    fill(b, b + sqvalmax + 1, 0);
    for (int i = 1; i <= n; ++i) {
        int x;
        fin >> x;
        a[x / sqvalmax]++;
        b[x % sqvalmax]++;
    }
    int amax = max_element(a, a + sqvalmax + 1) - a;
    int bmax = max_element(b, b + sqvalmax + 1) - b;
    int res = amax * sqvalmax + bmax;
    fin.close(), fin.open("elmaj.in");
    fin >> n;
    int cnt = 0;
    for (int i = 1; i <= n; ++i) {
        int x;
        fin >> x;
        if (x == res) {
            cnt++;
        }
    }
    if (cnt > n / 2) {
        fout << res << cnt;
    }
    else {
        fout << -1;
    }
}