Cod sursa(job #1766414)

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

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

using namespace std;


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

class InputParser {
  public:
    inline fastcall 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();
    }

    inline fastcall 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();
    }
    inline fastcall ~InputParser(){
        if (streamIsCreated) delete m_stream;
    }

    inline fastcall 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;
    }
    inline fastcall 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;
    }
    inline fastcall InputParser& operator>>(char& chr) {
        chr = current();
        next();

        return *this;

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

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

  private:
    const int kBufferSize = 655536;

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

    inline fastcall 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:
    inline fastcall OutputParser(const string& fileName):
            m_pos(0),
            m_buffer(new char[kBufferSize])
        {
            m_stream = new ofstream(fileName);
            streamIsCreated = 1;
        }
    inline fastcall OutputParser(ostream& outputStream):
            m_pos(0),
            m_buffer(new char[kBufferSize])
        {
            m_stream = &outputStream;
            streamIsCreated = 0;
        }

    inline fastcall ~OutputParser(){
        Flush();
        if (streamIsCreated) delete m_stream;
    }
    inline fastcall 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;
    }
    inline fastcall 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;
    }

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

        if (m_pos == kBufferSize) Flush();

        return *this;
    }

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

    inline fastcall 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;

    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;
}