Cod sursa(job #2619190)

Utilizator AlexVulpoiuAlexandru Vulpoiu AlexVulpoiu Data 27 mai 2020 11:23:28
Problema Radix Sort Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.15 kb
#include <fstream>
#include <algorithm>

using namespace std;

int aux[10000005], v[10000005];

void countsort(int w[], const int& m, const int& p)
{
    int i, fr[65540];

    for(i = 0; i < 65536; i++)
        fr[i] = 0;
    for(i = 0; i < m; i++)
        fr[(w[i] / p) & 65535]++;
    for(i = 1; i < 65536; i++)
        fr[i] += fr[i - 1];
    for(i = m - 1; i >= 0; i--)
    {
        aux[fr[(w[i] / p) & 65535] - 1] = w[i];
        fr[(w[i] / p) & 65535]--;
    }
    for(i = 0; i < m; i++)
        w[i] = aux[i];
}

void radixsort(int w[], const int& m)
{
    int i, max1;

    max1 = w[0];
    for(i = 1; i < m; i++)
        max1 = max(max1, w[i]);
    countsort(w, m, 1);
    countsort(w, m, 65536);
}

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

    int n, a, b, c, i;

    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(v, n);

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

    return 0;
}