Cod sursa(job #1657977)

Utilizator dm1sevenDan Marius dm1seven Data 20 martie 2016 22:22:42
Problema Elementul majoritar Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 2.87 kb
#if 1
#define _USE_MATH_DEFINES
#include <math.h>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <utility>
#include <sstream>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <fstream>
#include <chrono>
using namespace std;
using namespace std::chrono;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vint;
typedef vector<vint> vvint;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vvll> vvvll;
typedef vector<ull> vull;
typedef vector<vull> vvull;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<string> vstring;
typedef vector<double> vdouble;

#define fs first
#define sc second
#define pb push_back
// forward strict for, most common
#define fors(i, a, n) for (int i = (int) (a); i < (int) (n); ++i)
// forward inclusive for
#define fori(i, a, n) for (int i = (int) (a); i <= (int) (n); ++i)
// backward for, inclusive
#define forb(i, n, a) for (int i = (int) (n); i >= (a); --i)

template <typename T>
void print(string name, vector<T>& v) {
    if (name.size()) cout << name << ": ";
    int s = (int) v.size();
    fors(i, 0, s) cout << v[i] << " ";
    cout << endl;
}
template<typename T> void print(vector<T>& v) { print("", v); }
#endif

int main() {
    ifstream in("elmaj.in");
    ofstream out("elmaj.out");
    
    int n;
    in >> n;
    int hsz = 4 * 1000;
    vector<map<int, int>> vmp(2 * 1000000000 / hsz + 1);
    fors(i, 0, n) {
        int a;
        in >> a;
        vmp[a/hsz][a]++;
    }
    pair<int, int> rp(0, 0);
    fors(i, 0, vmp.size()) {
        for (auto p : vmp[i]) {
            if (p.sc > rp.sc) rp = p;
        }
    }
    
    if (rp.sc > n/2) out << rp.fs << " " << rp.sc << endl;
    else out << -1 << endl;
    
    return 0;
}

int main1() {
    ifstream in("elmaj.in");
    ofstream out("elmaj.out");
    
    int n;
    in >> n;
    map<int, int> mp;
    fors(i, 0, n) {
        int a;
        in >> a;
        mp[a]++;
    }
    pair<int, int> rp = *mp.begin();
    for(auto p : mp) {
        if (p.sc > rp.sc) rp = p;
    }
    
    if (rp.sc > n/2) out << rp.fs << " " << rp.sc << endl;
    else out << -1 << endl;
    
    return 0;
}

int main2() {
    ifstream in("elmaj.in");
    ofstream out("elmaj.out");
    
    ll n;
    in >> n;
    vll v(n);
    fors(i, 0, n) in >> v[i];
    sort(v.begin(), v.end());
    ll c = 1;
    ll bc = 1;
    ll bi = 0;
    fors(i, 1, n) {
        if (v[i] == v[i-1]) {
            c++;
        } else {
            if (c > bc) bc = c, bi = i - 1;
            c = 1;
        }
    }
    
    if (bc > n / 2) out << v[bi] << " " << bc << endl;
    else out << -1;
    
    return 0;
}