Cod sursa(job #2352169)

Utilizator Kln1000Ciobanu Bogdan Kln1000 Data 23 februarie 2019 01:36:14
Problema Sortare prin comparare Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 6.92 kb
/*#include <fstream>
#include <bits/stdc++.h>
#include <thread>

#define DEBUG
#define SIGN

#define endl "\n"

#pragma GCC poison scanf printf qsort

#pragma GCC optimize(3)
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;
*/
/*class alignas(512) parser_faux{
public:
    static std::fstream in_file;
    static const int SIZE = BUFSIZ * 1024,
                     SECT_SIZE = 512;
    static int index, sign, sector;
    static char main_buffer[SIZE],
                aux_buffer[SECT_SIZE];
#ifdef DEBUG
    static constexpr auto inc = [&] () -> void { 
        if (++index == SECT_SIZE) {
            index &= 0;
            in_file.read(aux_buffer, SECT_SIZE);
            if (++sector == SIZE) {
                sector &= 0;
            }
        }
    };
#else
    void inc() {
        if (++index == SIZE) { 
            index &= 0;
            in_file.read(aux_buffer, SECT_SIZE);
            if (++sector == SIZE) {
                sector &= 0;
        }
    }
#endif /// DEBUG
public:
    parser () {
    } /// c-tor
    parser (const char *file_name) {
        in_file.open(file_name, std::ios_base::in | std::ios_base::binary),
        in_file.rdbuf() -> pubsetbuf(main_buffer, SIZE),
        in_file.sync_with_stdio(false),
        in_file.read(aux_buffer, SECT_SIZE),
        index &= 0,
        sector &= 0;
    }
    [[gnu::pure]] inline parser &operator >> (int &n) {
        for (; aux_buffer[index] < '0' and aux_buffer[index] > '9'; inc());
        n &= 0;
#ifdef SIGN
        sign &= 0,
        sign |= (aux_buffer[index - 1] == '-');
#endif /// SIGN
        for (; aux_buffer[index] >= '0' and aux_buffer[index] <= '9'; inc())
            n = (n << 3) + (n << 1) + aux_buffer[index] - '0';
#ifdef SIGN
        n ^= ((n ^ -n) & -sign);
#endif /// SIGN
        return *this;
    }
    ~parser () {
        in_file.close(); 
    } /// d-tor
};*/
/*
#pragma pack (push, 1)

class alignas(512) parser : public std::streambuf {
   public:
        explicit parser() { /// default c-tor
        /// Copyright Ciobanu Bogdan-Calin™ 2019
    }
        explicit parser(const char *file_name) {
           input_file.open(file_name, ios::in | ios::binary);
           input_file.sync_with_stdio(false);
           index &= 0;
           input_file.read(buffer, SIZE);
    } /// main-sequence c-tor
        [[gnu::hot]] inline parser &operator >> (int &n) {
            for (;buffer[index] < '0' or buffer[index] > '9'; inc());
            n &= 0;
            sign &= 0;
            sign |= (buffer[index - 1] == '-');
            for (;'0' <= buffer[index] and buffer[index] <= '9'; inc())
                n= 10 * n + buffer[index] - '0';
            n ^= ((n ^ -n) & -sign);
            return *this;
        }
        ~parser() {
            input_file.close();
        }
    private:
        fstream input_file;
        static const size_t SIZE = 0x400000, /// 4MB
                         SECT_SIZE = 4096;
        char main_buffer[SIZE],
             buffer[SECT_SIZE];
        int index, sign;
        inline void inc() {
            if(++index == SIZE)
                index=0,
                input_file.read(buffer, SIZE);
        }
};

#pragma pack (pop) 

int main() {
    parser f ("test.txt");
    int x;
    f >> x;
    cout << x << endl;
    return 0;
}
*/
#include <bits/stdc++.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>

class parser {
public:
    
    explicit parser () {
        /// default c-tor
    }
    
    explicit parser (const char *file_name) {
        fd = open(file_name, O_RDONLY, 0),
        block_number &= 0,
        index &= 0,
        buffer = (char*) mmap(NULL, SIZE, PROT_READ, MAP_SHARED, fd, 0);
    }
    [[gnu::hot]] inline parser &operator >> (int &n) {
        for (;buffer[index] < '0' or buffer[index] > '9'; inc());
        n &= 0;
#ifdef SIGNED
        sign &= 0;
        sign |= (buffer[index - 1] == '-');
#endif
        for (;'0' <= buffer[index] and buffer[index] <= '9'; inc())
            n= 10 * n + buffer[index] - '0';
#ifdef SIGNED
        n ^= ((n ^ -n) & -sign);
#endif
        return *this;
    }
    ~parser () {
        munmap(buffer, SIZE);
        close(fd);
    }
private:
    static const int SIZE = 0x400000; /// 4MB
    int fd, block_number, index;
    char *buffer;
#ifdef SIGNED
    int sign;
#endif
    [[gnu::hot]] inline void inc() {
        if(++index == SIZE)
            index &= 0,
            ++block_number,
            buffer = (char*) mmap(NULL, SIZE, PROT_READ, MAP_SHARED, fd, block_number * SIZE);
    }
};

class writer{
    public:
        writer() {};
        writer(const char *file_name){
            output_file.open(file_name,ios::out | ios::binary);
            output_file.sync_with_stdio(false);
            index=0;}
        inline writer &operator <<(int target){
            aux=0;
            n=target;
            if (!n)
                nr[aux++]='0';
            for (;n;n/=10)
                nr[aux++]=n%10+'0';
            for(;aux;inc())
                buffer[index]=nr[--aux];
            return *this;}
        inline writer &operator <<(const char *target){
            aux=0;
            while (target[aux])
                buffer[index]=target[aux++],inc();
            return *this;}
        ~writer(){
            output_file.write(buffer,index);output_file.close();}
    private:
        fstream output_file;
        static const int SIZE=0x200000;
        int index=0,aux,n;
        char buffer[SIZE],nr[24];
        inline void inc(){
            if(++index==SIZE)
                index=0,output_file.write(buffer,SIZE);}
};
 
parser f ("algsort.in");
writer t ("algsort.out");

int n,v[500010];

inline void insertionSort(int low,int up) {
    for (int i=low;i<up;++i){
        int x=v[i];
        int j=i;
        while ((j>low) and (v[j-1]>x)){
            v[j]=v[j-1];
            --j;
        }
        v[j]=x;
    }
}
 
void radixSort(int low, int up, int bits) {
    int ptr[1<<BITS],bucket[1<<BITS]={ };
    for (int i=low;i<up;++i)
        ++bucket[(v[i]>>bits)&MASK];
    ptr[0]=low;
    bucket[0]+=low;
    for (int i=1;i<(1<<BITS);++i){
        ptr[i]=bucket[i-1];
        bucket[i]+=bucket[i-1];
    }
    for (int i=0;i<(1<<BITS);++i){
        while (ptr[i]!=bucket[i]){
            int elem=v[ptr[i]];
            int bucket=(elem>>bits)&MASK;
            while (bucket!=i){
                int tmp=v[ptr[bucket]];
                v[ptr[bucket]++]=elem;
                elem=tmp;
                bucket=(elem>>bits)&MASK;
            }
        v[ptr[i]++]=elem;
        }
    }
    if (bits){
        for (int i=0;i<(1<<BITS);++i){
        int size=bucket[i]-(i?bucket[i-1]:low);
        if (size>THRESHOLD)
            radixSort(bucket[i]-size,bucket[i],bits-BITS);
        else if (size>1)
            insertionSort(bucket[i]-size,bucket[i]);
        }
    }
}
 
int main()
{
    f>>n;
    for (int i=0;i<n;++i)
        f>>v[i];
    radixSort(0,n,24);
    for (int i=0;i<n;++i)
        t<<v[i]<<" ";
    return 0;
}