Cod sursa(job #1468825)

Utilizator mihaiadelinamihai adelina mihaiadelina Data 7 august 2015 00:57:35
Problema Radix Sort Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include <fstream>
#include <vector>
using namespace std;

ifstream fin ("radixsort.in");
ofstream fout ("radixsort.out");

int a[10000001];
vector <int> bucket[256];

void radixSort (int n) {
    int i, r, k, j, l, buckCount[256], byteNo = 0;

    for (j = 0; j < 4; j++) {
        for (k = 0; k < 256; k++) {
            buckCount[k] = 0;
        }
        //Initialize bucket count;
        for (i = 0; i < n; i++) {
            r = (a[i] >> (byteNo * 8) & 255);
            if(buckCount[r] >= bucket[r].size()) {
                bucket[r].push_back(a[i]);
            }
            else {
                bucket[r][buckCount[r]] = a[i];
            }
            buckCount[r]++;
        }

        // colectam elementele din bucket
        for (k = 0, i = 0; k < 256; k++) {
            for (l = 0; l < buckCount[k]; l++)
                a[i++] = bucket[k][l];
        }
        byteNo++;
    }
}

int main() {
    int N, i;
    long long A, B, C;
    fin >> N >> A >> B >> C;

    a[0] = B;
    for (i = 1; i < N; i++) {
        a[i] = (int) ((A * a[i - 1] + B) % C);
    }

    radixSort(N);

    for (i = 0; i < N; i += 10) {
        fout << a[i] << " ";
    }

    return 0;
}