Cod sursa(job #1945193)

Utilizator Kln1000Ciobanu Bogdan Kln1000 Data 29 martie 2017 13:20:59
Problema Radix Sort Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 7.35 kb
#include <fstream>
#include <cstring>

#define BITS 8
#define MASK ((1<<BITS)-1)
#define THRESHOLD 64
#define MAX_N 10000000 + 1

using namespace std;

/**

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

*/

class parser{
    public:
        parser() {}
        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);}
        inline parser &operator >>(int &n){
            for (;buffer[index]<'0' or buffer[index]>'9';inc());
            n=0;
            for (;'0'<=buffer[index] and buffer[index]<='9';inc())
                n=10*n+buffer[index]-'0';
            return *this;}
            ~parser(){
            input_file.close();}
    private:
        fstream input_file;
        static const int SIZE=0x400000;
        char buffer[SIZE];
        int index=0;
        inline void inc(){
            if(++index==SIZE)
                index=0,input_file.read(buffer,SIZE);}
};

class Writer {
  public:
    Writer(const char *name){
        m_stream.open(name,ios::out | ios::binary);
        //m_stream.sync_with_stdio(false);
        memset(m_buffer, 0, sizeof(m_buffer));
        m_pos = 0;
    }

    Writer& operator<<(int a) {
        int many = 0;
        do {
            digit_buffer[many++] = a % 10 + '0';
            a /= 10;
        } while (a > 0);
        for (int i = many - 1; i >= 0; --i)
            putchar(digit_buffer[i]);
        return *this;
    }

    Writer& operator<<(const char *s) {
        for (; *s; ++s)
            putchar(*s);
        return *this;
    }

    ~Writer() {
        m_stream.write(m_buffer, m_pos);
    }

  private:
    void putchar(char c) {
        m_buffer[m_pos++] = c;
        if (m_pos == kBufferSize) {
            m_stream.write(m_buffer, m_pos);
            m_pos = 0;
        }
    }

    static const int kBufferSize = 0x200000;
    fstream m_stream;
    char m_buffer[kBufferSize];
    char digit_buffer[30];
    int m_pos;
} t ("radixsort.out");

/*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 ("radixsort.in");

int n,v[MAX_N];

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()
{
    int a,b,c;
    f>>n>>a>>b>>c;
    v[0]=b;
    for (int i=1;i<n;++i){
        int64_t aux=1LL*v[i-1]*a+b;
        v[i]=aux-(aux/c)*c;
    }
    radixSort(0,n,24);
    for (int i=0;i<n;i+=10)
        t<<v[i]<<" ";
    return 0;
}