Cod sursa(job #2803467)

Utilizator Tudor06MusatTudor Tudor06 Data 20 noiembrie 2021 01:20:03
Problema Radix Sort Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.27 kb
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>

using namespace std;

const int NMAX = 1e7;
const int BASE = 2048;

int n;
int v[2][NMAX];
int frecv[2048];

inline void reorder( int xit, int aux ) {
    int i;
    memset( frecv, 0, sizeof( frecv ) );
    for ( i = 0; i < n; i ++ )
        frecv[( v[1 - aux][i] >> xit ) % 2048] ++;
    for ( i = 1; i < 2048; i ++ )
        frecv[i] += frecv[i - 1];
    for ( i = n - 1; i >= 0; i -- )
        v[aux][--frecv[( v[1 - aux][i] >> xit ) % 2048]] = v[1 - aux][i];
}
inline void radix() {
    int xit;
    for ( xit = 0; xit < 2; xit ++ ) {
        reorder( 16 * xit, 1 - xit );
    }
}
class OutParser {
private:
    FILE *fout;
    char *buff;
    int sp;

    void write_ch(char ch) {
        if (sp == 50000) {
            fwrite(buff, 1, 50000, fout);
            sp = 0;
            buff[sp++] = ch;
        } else {
            buff[sp++] = ch;
        }
    }


public:
    OutParser(const char* name) {
        fout = fopen(name, "w");
        buff = new char[50000]();
        sp = 0;
    }
    ~OutParser() {
        fwrite(buff, 1, sp, fout);
        fclose(fout);
    }

    OutParser& operator << (int vu32) {
        if (vu32 <= 9) {
            write_ch(vu32 + '0');
        } else {
            (*this) << (vu32 / 10);
            write_ch(vu32 % 10 + '0');
        }
        return *this;
    }

    OutParser& operator << (long long vu64) {
        if (vu64 <= 9) {
            write_ch(vu64 + '0');
        } else {
            (*this) << (vu64 / 10);
            write_ch(vu64 % 10 + '0');
        }
        return *this;
    }

    OutParser& operator << (char ch) {
        write_ch(ch);
        return *this;
    }
    OutParser& operator << (const char *ch) {
        while (*ch) {
            write_ch(*ch);
            ++ch;
        }
        return *this;
    }
};
int main() {
    ifstream fin( "radixsort.in" );
    OutParser fout( "radixsort.out" );
    int a, b, c, i;
    fin >> n >> a >> b >> c;
    v[0][0] = b;
    for ( i = 1; i < n; i ++ ) {
        v[0][i] = ( (long long)a * v[0][i - 1] + b ) % c;
    }

    radix();

    for ( i = 0; i < n; i += 10 )
        fout << v[0][i] << ' ';
    return 0;
}