Cod sursa(job #1786223)

Utilizator elffikkVasile Ermicioi elffikk Data 22 octombrie 2016 16:51:56
Problema Radix Sort Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 0.93 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

main() {
    ifstream cin("radixsort.in");
    ofstream cout("radixsort.out");
    unsigned int n, a, b, c;
    cin>>n>>a>>b>>c;
    //sqrtN buckets
    unsigned int kn = round(sqrt(c))+100;
    vector< vector<unsigned int> > t(kn);
    //generate vector
    unsigned int x = b;
    t[x/kn].push_back(x);
    for (int i = 1; i<n; i++) {
        x = (a * x + b) % c;
        t[x/kn].push_back(x);
    }
    //sort buckets
    for (int i = 0; i < t.size(); i++) {
        sort(t[i].begin(), t[i].end());
    }
    //show
    int k = 0;
    for (int i = 0; i < t.size(); i++) {
        if (t[i].size()) {
            for (int j = 0; j < t[i].size(); j++) {
                k++;
                if (k%10==1) {
                    cout<<t[i][j]<<" ";
                }
            }
        }
    }

}