Cod sursa(job #3220804)

Utilizator DobraVictorDobra Victor Ioan DobraVictor Data 4 aprilie 2024 21:00:37
Problema Radix Sort Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.33 kb
#include <iostream>
#include <fstream>
#include <stdint.h>

const int64_t WORD_SIZE_LOG_2 = 8;
const int64_t WORD_SIZE = 1 << WORD_SIZE_LOG_2;
const int64_t WORD_COUNT = 4;
const int64_t MAX_N = 10000000;

int64_t vec[MAX_N];
int64_t vec2[MAX_N];
int main() {
    std::ifstream fin("radixsort.in");
    std::ofstream fout("radixsort.out");

    int64_t n, a, b, c;
    fin >> n >> a >> b >> c;

    vec[0] = b;
    for(int64_t i = 1; i != n; ++i)
        vec[i] = (a * vec[i - 1] + b) % c;
    
    for(int64_t i = 0; i != WORD_COUNT; ++i) {
        int64_t starts[WORD_SIZE];
        for(int64_t j = 0; j != WORD_SIZE; ++j)
            starts[j] = 0;
        
        for(int64_t j = 0; j != n; ++j) {
            int64_t word = (vec[j] >> (WORD_SIZE * i)) & (WORD_SIZE - 1);

            if(word != WORD_SIZE - 1)
                ++starts[word + 1];
        }
        for(int64_t j = 2; j != WORD_SIZE; ++j)
            starts[j] += starts[j - 1];
        
        for(int64_t j = 0; j != n; ++j) {
            int64_t word = (vec[j] >> (WORD_SIZE * i)) & (WORD_SIZE - 1);

            vec2[starts[word]++] = vec[j];
        }
        for(int64_t j = 0; j != n; ++j)
            vec[j] = vec2[j];
    }

    for(int64_t i = 0; i < n; i += 10)
        fout << vec[i] << ' ';

    fin.close();
    fout.close();
}