Cod sursa(job #2642506)

Utilizator LivcristiTerebes Liviu Livcristi Data 15 august 2020 18:40:05
Problema Radix Sort Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.23 kb
#include <bits/stdc++.h>
#define NUM 10000005
#define BYTE 8
#define BUCK 256
using namespace std;
int v[NUM];
int temp[NUM];
int n, a, b, c;

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

void ByteSort(int * prim, int * sec, int byte)
{
    int counter[BUCK] = {0};
    int index[BUCK] = {0};

    for(int i = 0; i < n; ++i)
        ++counter[(prim[i] >> byte) & 255];

    for(int i = 1; i < BUCK; ++i)
        index[i] = index[i - 1] + counter[i - 1];

    for(int i = 0; i < n; ++i)
        sec[index[(prim[i] >> byte) & 255]++] = prim[i];
}
void radixSort()
{
    ByteSort(v, temp,        0);
    ByteSort(temp, v,     BYTE);
    ByteSort(v, temp, 2 * BYTE);
    ByteSort(temp, v, 3 * BYTE);
}
int main()
{
    ifstream f("radixsort.in");
    OutParser g("radixsort.out");

    f >> n >> a >> b >> c;
    v[0] = b;
    for(int i = 1; i < n; ++i)
        v[i] = (1LL * a * v[i - 1] % c + b) % c;

    radixSort();

    for(int i = 0; i < n; i += 10)
        g << v[i] << ' ';
    f.close();
    return 0;
}