Cod sursa(job #1167758)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 5 aprilie 2014 21:16:47
Problema Radix Sort Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.14 kb
#include <fstream>
#include <vector>

using namespace std;

const int BIT_COUNT = 8;
const int MASK = (1 << BIT_COUNT) - 1;
const int MAX_BITS = 32;
const int MAX_N = 10000000;

int N, Values[MAX_N];

void RadixSort(const int n, int values[], const int shift = 0) {
    if (shift >= MAX_BITS)
        return;
    vector<int> buckets[MASK + 1];
    for (int i = 0; i < n; ++i)
        buckets[(values[i] >> shift) & MASK].push_back(values[i]);
    for (int b = 0, i = 0; b <= MASK; ++b)
        for (vector<int>::const_iterator v = buckets[b].begin(); v != buckets[b].end(); ++v)
            values[i++] = *v;
    RadixSort(n, values, shift + BIT_COUNT);
}

void Read() {
    ifstream cin("radixsort.in");
    int a, b, c;
    cin >> N >> a >> b >> c;
    Values[0] = b;
    for (int i = 1; i < N; ++i)
        Values[i] = (1LL * Values[i - 1] * a + b) % c;
    cin.close();
}

void Print() {
    ofstream cout("radixsort.out");
    for (int i = 0; i < N; i += 10)
        cout << Values[i] << " ";
    cout << "\n";
    cout.close();
}

int main() {
    Read();
    RadixSort(N, Values);
    Print();
    return 0;
}