Cod sursa(job #1860023)

Utilizator FlorinHajaFlorin Gabriel Haja FlorinHaja Data 27 ianuarie 2017 21:19:54
Problema Radix Sort Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.05 kb
#include <fstream>
#include <cstring>
#define Nbytes sizeof(nmb[0])
#define Rad_filter 0xff
#define Rad_size 8
#define split(x) ((x >> (byte*Rad_size))&Rad_filter)

using namespace std;

ifstream f("radixsort.in");
ofstream g("radixsort.out");

int nmb[10000005], n, i, j, a, b, c;

void soort(int in[], int out[], int byte) {
    int i;
    int cnt[1<<Rad_size], ind[1<<Rad_size];
    memset(cnt,0,sizeof(cnt));
    for (i = 0; i < n; i++)
        cnt[split(in[i])]++;
    ind[0] = 0;
    for (i = 1; i < (1<<Rad_size); i++)
        ind[i] = ind[i-1]+cnt[i-1];
    for (i = 0; i < n; i++)
        out[ind[split(in[i])]++] = in[i];
}

void radixsort() {
    int i;
    int *tmp = new int[n];
    for (i = 0; i < Nbytes; i++)
        if (i%2)
            soort(tmp,nmb,i);
        else soort(nmb,tmp,i);

}

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

    radixsort();
    for (i = 0; i < n; i += 10)
        g << nmb[i] << ' ';
    return 0;
}