Cod sursa(job #3220808)

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

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

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

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

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

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

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

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

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

	return 0;
}