Cod sursa(job #2769516)

Utilizator victorgoreanuGoreanu Victor victorgoreanu Data 16 august 2021 14:16:13
Problema Elementul majoritar Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.2 kb
#include <bits/stdc++.h>
using namespace std;
string __fname = "elmaj"; ifstream in (__fname + ".in"); ofstream out (__fname + ".out"); 
#define cin in 
#define cout out
vector <int> a;

int binarys(int target){
    int l = 0, r = a.size(), mid = 0;
    while (l <= r){
        mid = l + (r - l) / 2;
        if (a[mid] == target) return mid;
        if (a[mid] > target) r = mid - 1;
        if (a[mid] < target) l = mid + 1;
    }
    return -1;
}

int count(int target){
    int count = 1;
    int c = binarys(target);
    if (c == -1) return -1;
    else {
        int y = target - 1;
        while (a[y] == target){
            count++;
            y--;
        }
        int u = target + 1;
        while (a[u] == target){
            count++;
            u++;
        }
    }
    return count;
}

int main(){
    int n;
    cin >> n;
    for (int i = 0; i < n; i++){
        int t;
        cin >> t;
        a.push_back(t);
    }
    sort (a.begin(), a.end());
    int b = -2, c = -2;
    for (int i = 0; i < n; i++){
        int r = count(a[i]);
        if (r > c){
            b = i;
            c = r;
        }
    }
    cout << a[b] << " " << c;
    return 0;
}