Cod sursa(job #1167763)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 5 aprilie 2014 21:22:24
Problema Radix Sort Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.12 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 shift) {
    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;
}

void RadixSort() {
    for (int shift = 0; shift < MAX_BITS; shift += BIT_COUNT)
        RadixSort(shift);
}

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();
    Print();
    return 0;
}