Cod sursa(job #2473931)

Utilizator Kln1000Ciobanu Bogdan Kln1000 Data 14 octombrie 2019 15:05:50
Problema Sortare prin comparare Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 6.17 kb
#include <fstream>
#include <iostream>

#include <vector>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>

using namespace std;

/**

                                          `-.`'.-'
                                       `-.        .-'.
                                    `-.    -./\.-    .-'
                                        -.  /_|\  .-
                                    `-.   `/____\'   .-'.
                                 `-.    -./.-""-.\.-      '
                                    `-.  /< (()) >\  .-'
                                  -   .`/__`-..-'__\'   .-
                                ,...`-./___|____|___\.-'.,.
                                   ,-'   ,` . . ',   `-,
                                ,-'   ________________  `-,
                                   ,'/____|_____|_____\
                                  / /__|_____|_____|___\
                                 / /|_____|_____|_____|_\
                                ' /____|_____|_____|_____\
                              .' /__|_____|_____|_____|___\
                             ,' /|_____|_____|_____|_____|_\
,,---''--...___...--'''--.. /../____|_____|_____|_____|_____\ ..--```--...___...--``---,,
                           '../__|_____|_____|_____|_____|___\
      \    )              '.:/|_____|_____|_____|_____|_____|_\               (    /
      )\  / )           ,':./____|_____|_____|_____|_____|_____\             ( \  /(
     / / ( (           /:../__|_____|_____|_____|_____|_____|___\             ) ) \ \
    | |   \ \         /.../|_____|_____|_____|_____|_____|_____|_\           / /   | |
 .-.\ \    \ \       '..:/____|_____|_____|_____|_____|_____|_____\         / /    / /.-.
(=  )\ `._.' |       \:./ _  _ ___  ____  ____ _    _ _ _ _ _  _ __\        | `._.' /(  =)
 \ (_)       )        \/                                            \       (       (_) /
  \    `----'          """"""""""""""""""""""""""""""""""""""""""""""        `----'    /
   \   ____\__                                                              __/____   /
    \ (=\     \                                                            /     /-) /
     \_)_\     \                                                         /     /_(_/
          \     \                                                        /     /
           )     )  _                                                _  (     (
          (     (,-' `-..__                                    __..-' `-,)     )
           \_.-''          ``-..____                  ____..-''          ``-._/
            `-._                    ``--...____...--''                    _.-'
                `-.._                                                _..-'
                     `-..__              OPUS DEI              __..-'
                           ``-..____                  ____..-''
                                    ``--...____...--''

*/

class writer {
public:
    writer() {};

    writer(const char* file_name) {
        output_file.open(file_name, std::ios::out | std::ios::binary);
        output_file.sync_with_stdio(false);
        index &= 0;
    }

    template <typename T>
    inline writer& operator <<(T target) {
        auto inc = [&, this]() mutable {
            if (++index == SIZE)
                index &= 0,
                output_file.write(buffer, SIZE);
        };

        aux &= 0;
        if (target == 0) {
            buffer[index] = '0';
            inc();
            return *this;
        }
#pragma GCC ivdep
        for (; target; target = target / 10)
            nr[aux++] = target % 10 + '0';
#pragma GCC ivdep
        for (; aux; inc())
            buffer[index] = nr[--aux];
        return *this;
    }

    inline writer& operator <<(const char* target) {
        auto inc = [&, this]() mutable {
            if (++index == SIZE)
                index &= 0,
                output_file.write(buffer, SIZE);
        };

        aux &= 0;
        while (target[aux])
            buffer[index] = target[aux++], inc();
        return *this;
    }

    inline void close() {
        delete this;
    }

    ~writer() {
        output_file.write(buffer, index);
        output_file.close();
    }

private:
    static const int SIZE = 0x300000;
    int index, aux;
    char buffer[SIZE], nr[24];
    std::fstream output_file;
};

class mapping_parser {
public:
    inline mapping_parser() {
        /// default c-tor
    }

    inline mapping_parser(const char* file_name) {
        int fd = open(file_name, O_RDONLY);
        index &= 0;
        fstat(fd, &sb);
        buffer = (char*)mmap(0, sb.st_size, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, fd, 0);
        close(fd);
    }

    template <typename T>
    inline mapping_parser& operator >> (T& n) {
        n &= 0;
#pragma GCC ivdep
        for (; buffer[index] < '0' or buffer[index] > '9'; ++index);
#pragma GCC ivdep
        for (; '0' <= buffer[index] and buffer[index] <= '9'; ++index)
            n = (n << 3) + (n << 1) + buffer[index] - '0';
        return *this;
    }

    ~mapping_parser() {
        munmap(buffer, sb.st_size);
    }

private:
    struct stat sb;
    int index;
    char* buffer;
};

mapping_parser f("algsort.in");
writer t("algsort.out");

#define WORD_SIZE 256

inline void radixSort(unsigned n, unsigned* A, unsigned* B, unsigned digit) {
    unsigned frequence[WORD_SIZE]{}, index[WORD_SIZE];
    index[0] = ~0;

#pragma GCC ivdep
    for (unsigned i = n; i;)
        ++frequence[A[--i] >> digit & (WORD_SIZE - 1)];

    for (unsigned i = 1; i < WORD_SIZE; ++i) {
        index[i] = index[i - 1] + frequence[i - 1];
    }

#pragma GCC ivdep
    for (unsigned i = 0; i < n; ++i)
        B[++index[A[i] >> digit & (WORD_SIZE - 1)]] = A[i];
}

int main() {
    unsigned n, v[500010], w[500010];
    f >> n;
#pragma GCC ivdep
    for (uint32_t i = 0; i < n; ++i)
        f >> v[i];

    radixSort(n, v, w, 0);
    radixSort(n, w, v, 8);
    radixSort(n, v, w, 16);
    radixSort(n, w, v, 24);

#pragma GCC ivdep
    for (uint32_t i = 0; i < n; ++i)
        t << v[i] << " ";

    return 0;
}