Cod sursa(job #2619523)

Utilizator AlexVulpoiuAlexandru Vulpoiu AlexVulpoiu Data 27 mai 2020 21:11:08
Problema Radix Sort Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.99 kb
#include <fstream>
#include <algorithm>

using namespace std;

ifstream f("radixsort.in");
ofstream g("radixsort.out");

int n, a, b, c, i, aux[10000005], v[10000005];

void countsort(int v1[], int v2[], const int& p)
{
    int i, fr[260];

    for(i = 0; i < 256; i++)
        fr[i] = 0;
    for(i = 0; i < n; i++)
        fr[(v1[i] >> p) & 255]++;
    for(i = 1; i < 256; i++)
        fr[i] += fr[i - 1];
    for(i = n - 1; i >= 0; i--)
        v2[--fr[(v1[i] >> p) & 255]] = v1[i];
}

void radixsort()
{
    int i;

    for(i = 0; i < 4; i++)
        if(i & 1)
            countsort(aux, v, i * 8);
        else
            countsort(v, aux, i * 8);
}

int main()
{
    f >> n >> a >> b >> c;
    f.close();

    v[0] = b;
    for(i = 1; i < n; i++)
        v[i] = (int)(((1LL * a * (1LL * v[i - 1])) % (1LL * c) + 1LL * b) % (1LL * c));

    radixsort();

    for(i = 0; i < n; i += 10)
        g << v[i] << ' ';
    g << '\n';
    g.close();

    return 0;
}