Cod sursa(job #1766253)

Utilizator cosmin.pascaruPascaru Cosmin cosmin.pascaru Data 27 septembrie 2016 19:15:47
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 6.29 kb
#include <stdio.h>
#include <bits/stdc++.h>

const int kMaxN = 100000;
const int kBuffSize = 655536;

#define fastcall __attribute__((optimize("-O3")))
#define inline __inline__ __attribute__((always_inline))

int T[2 * kMaxN];

inline fastcall char getChar() {
    static char buff[kBuffSize];
    static int pos = kBuffSize;

    if (pos == kBuffSize) {
        fread(buff, 1, kBuffSize, stdin);
        pos = 0;
    }
    return buff[pos++];
}

inline fastcall int readInt() {
    int q = 0;
    char c;
    do {
        c = getChar();
    } while (!isdigit(c));
    do {
        q = (q << 1) + (q << 3) + (c - '0');
        c = getChar();
    } while (isdigit(c));
    return q;
}

char outBuff[kBuffSize];
int outPtr;

inline fastcall void putChar(const char &C) {
    outBuff[outPtr++] = C;
    if (outPtr == kBuffSize) {
        fwrite(outBuff, 1, kBuffSize, stdout);
        outPtr = 0;
    }
}

inline fastcall void writeInt(int X) {
    char digs[10];
    int n = 0, q;
    do {
        q = X / 10;
        digs[n++] = X - (q << 1) - (q << 3) + '0';
        X = q;
    } while (X);
    while (n--) {
        putChar(digs[n]);
    }
    //putChar('\n');
}

#include <bits/stdc++.h>

using namespace std;


/// ----------------------------------------- ///
/// ------------- INPUT PARSER -------------- ///
/// ----------------------------------------- ///

class InputParser {
  public:
    InputParser(const string& fileName):
            m_pos(kBufferSize - 1),
            m_buffer(new char[kBufferSize]) {

        ios_base::sync_with_stdio(0);
        cin.tie(0);

        m_stream = new ifstream(fileName);
        streamIsCreated = 1;
        next();
    }

    InputParser(istream& inputStream):
            m_pos(kBufferSize - 1),
            m_buffer(new char[kBufferSize]) {

        m_stream = &inputStream;
        streamIsCreated = 0;

        ios_base::sync_with_stdio(0);
        cin.tie(0);
        next();
    }
    ~InputParser(){
        if (streamIsCreated) delete m_stream;
    }

    InputParser& operator>>(int& value) {
        value = 0;
        while (current() < '0' || current() > '9')
            next();
        while (current() >= '0' && current() <= '9') {
            value = (value << 3) + (value << 1) + current() - '0';
            next();
        }
        return *this;
    }
    InputParser& operator>>(long long& value) {
        value = 0;
        while (current() < '0' || current() > '9')
            next();
        while (current() >= '0' && current() <= '9') {
            value = value * 10 + current() - '0';
            next();
        }
        return *this;
    }
    InputParser& operator>>(char& chr) {
        chr = current();
        next();

        return *this;

    }
    int getline(char* str, int max_length){
        int lg = 0;
        while (current() != '\n' && lg < max_length){
            str[lg++] = current();
            next();
        }
        str[lg] = NULL;
        next();

        return lg;
    }
    char get(){
        char c;
        c = current();
        next();
        return c;
    }
    void ignore(){
        next();
    }

  private:
    const int kBufferSize = 655536;

    inline char current() {
        return m_buffer[m_pos];
    }

    void next() {
        if (++m_pos >= kBufferSize) {
            m_stream->read(m_buffer.get(), kBufferSize);
            m_pos = 0;
        }
    }

    istream* m_stream;
    int streamIsCreated;
    int m_pos;
    unique_ptr<char[]> m_buffer;
};

/// ----------------------------------------- ///
/// ------------ OUTPUT PARSER -------------- ///
/// ----------------------------------------- ///

class OutputParser {
  public:
    OutputParser(const string& fileName):
            m_pos(0),
            m_buffer(new char[kBufferSize])
        {
            m_stream = new ofstream(fileName);
            streamIsCreated = 1;
        }
    OutputParser(ostream& outputStream):
            m_pos(0),
            m_buffer(new char[kBufferSize])
        {
            m_stream = &outputStream;
            streamIsCreated = 0;
        }

    ~OutputParser(){
        Flush();
        if (streamIsCreated) delete m_stream;
    }
    OutputParser& operator<<(int value) {
        char digits[10];
        int nr = 0, q;
        do {
            q = value / 10;
            digits[nr++] = value - (q << 1) - (q << 3) + '0';
            value = q;
        } while (value);

        while (nr--) {
            m_buffer[m_pos++] = digits[nr];
            if (m_pos == kBufferSize) Flush();
        }

        return *this;
    }
    OutputParser& operator<<(long long value) {
        char digits[20];
        int nr = 0, q;
        do {
            q = value / 10;
            digits[nr++] = value - (q << 1) - (q << 3) + '0';
            value = q;
        } while (value);

        while (nr--) {
            m_buffer[m_pos++] = digits[nr];
            if (m_pos == kBufferSize) Flush();
        }

        return *this;
    }

    OutputParser& operator<<(char value) {
        m_buffer[m_pos++] = value;

        if (m_pos == kBufferSize) Flush();

        return *this;
    }

    OutputParser& operator<<(char* str) {
        while (*str){
            m_buffer[m_pos++] = *str;
            ++str;
            if (m_pos == kBufferSize) Flush();
        }
        return *this;
    }

    void Flush(){
        m_stream->write(m_buffer.get(), m_pos);
        m_pos = 0;
    }

  private:
    const int kBufferSize = 65536;

    ostream* m_stream;
    int streamIsCreated;
    int m_pos;
    unique_ptr<char[]> m_buffer;
};


#define NMAX 1000005
using namespace std;

int a[NMAX];
int main()
{
    InputParser fin("elmaj.in");
    OutputParser fout("elmaj.out");

    int n, cand, k, x;

    fin >> n;
    fin >> a[1]; cand = a[1]; k = 1;
    for (int i = 2; i <= n; ++i)
    {
        fin >> a[i];
        if (a[i] == cand) ++k;
        else if (k == 0)
        {
            cand = a[i];
            ++k;
        }
            else --k;
    }
    k = 0;
    for (int i = 1; i <= n; ++i)
        if (a[i] == cand) ++k;
    if (k > n/2) fout << cand << ' ' << k << '\n';
    else fout << "-1" << '\n';
    return 0;
}