Cod sursa(job #1367020)

Utilizator viuscenkoViktor Iuscenko viuscenko Data 1 martie 2015 15:54:56
Problema Elementul majoritar Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.43 kb
#include <bits/stdc++.h>

using namespace std;

#define     mp              make_pair
#define     fs              first
#define     sc              second
#define     pob             pop_back
#define     pub             push_back
#define     eps             1E-7
#define     sz(a)           a.size()
#define     count_one       __builtin_popcount;
#define     count_onell     __builtin_popcountll;
#define     fastIO          ios_base::sync_with_stdio(false)
#define     PI              (acos(-1.0))
#define     linf            (1LL<<62)//>4e18
#define     inf             (0x7f7f7f7f)//>2e9

#define DEBUG 1
#ifdef DEBUG
#define D(x) x
#else
#define D(x)
#endif

FILE *in = fopen("elmaj.in", "r");
FILE *out = fopen("elmaj.out", "w");

int n, elmaj, elcount;
fpos_t pos;

int main()
{
    int x;
    elcount = 1;

    fscanf(in, "%d", &n);

    fgetpos(in, &pos);
    fscanf(in, "%d", &elmaj);
    for(int i = 1; i < n; ++i) {
        fscanf(in, "%d", &x);
        if(x == elmaj)
            elcount++;
        else if(elcount > 0)
            elcount--;
        else
            elmaj = x, elcount = 1;
    }

    fsetpos(in, &pos);
    elcount = 0;
    for(int i = 0; i < n; ++i) {
        fscanf(in, "%d", &x);
        if(x == elmaj)
            elcount++;
    }
    if(elcount > (n >> 1))
        fprintf(out, "%d %d\n", elmaj, elcount);
    else
        fprintf(out, "-1\n");
    return 0;
}