Cod sursa(job #1806984)

Utilizator andreigasparoviciAndrei Gasparovici andreigasparovici Data 15 noiembrie 2016 21:24:05
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.6 kb
#include <cstdio>
#include <map>
#include <vector>
using namespace std;
map<int,int>m;
int a[1000000];
int n;
#include <stdio.h>
#include <ctype.h>

class InParser {
private:
    FILE *fin;
    char *buff;
    int sp;

    char read_ch() {
        ++sp;
        if (sp == 4096) {
            sp = 0;
            fread(buff, 1, 4096, fin);
        }
        return buff[sp];
    }

public:
    InParser(const char* nume) {
        fin = fopen(nume, "r");
        buff = new char[4096]();
        sp = 4095;
    }

    InParser& operator >> (int &n) {
        char c;
        while (!isdigit(c = read_ch()) && c != '-');
        int sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }

    InParser& operator >> (long long &n) {
        char c;
        n = 0;
        while (!isdigit(c = read_ch()) && c != '-');
        long long sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }
};
int main()
{
    InParser inp("elmaj.in");
    inp>>n;
    for(int i=0;i<n;i++)
    {
        inp>>a[i];
        m[a[i]]++;
    }
    freopen("elmaj.out","w",stdout);
    for(int i=0;i<n;i++)
    {
        if(m[a[i]]>=n/2+1)
        {
            printf("%d %d",a[i],m[a[i]]);
            return 0;
        }
    }
    printf("-1");
    return 0;
}