Cod sursa(job #1785117)

Utilizator maryan_lupMarian Lupascu maryan_lup Data 20 octombrie 2016 21:26:33
Problema Radix Sort Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.46 kb
#include <iostream>
#include <fstream>
#include <iomanip>
#include <math.h>
#define DIMMAX 10000005
#define ll long long
#define un unsigned
#define FOR(i, a, b) for(un i = a; i <= b; i++)

using namespace std;

un v[DIMMAX], N, A, B, C, coada[10][DIMMAX];
un st[10], dr[10];

void enqueue(un nr,un poz)
{
    dr[poz]++;
    coada[poz][dr[poz]]=nr;
}

un dequeue(un poz)
{
    st[poz]++;
    un aux=coada[poz][st[poz]];
    return aux;
}

void citire()
{
    ifstream f("radixsort.in");
    f>>N>>A>>B>>C;
    f.close();
}

 void build_v()
 {
     v[1] = B;
     FOR(i,2,N)
     {
         v[i] = (A * v[i-1] + B) % C;
     }
 }

 void afisare()
 {
     ofstream g("radixsort.out");

     for(un i=1;i<=N;i+=10) //i+=10
     {
         g<<v[i]<<" ";
     }
     g.close();
 }

 un len()
 {
     un n=C,k=0;
     while(n)
     {
         k++;
         n/=10;
     }
     return k;
 }

 void radixsort()
 {
     un len_C=len();
     int k=1;

     FOR(k,1,len_C)
     {
         ll putere=pow(10,k-1);
         FOR(i,1,N)
         {
             un poz=(v[i]/putere)%10;
             enqueue(v[i],poz);
         }
         un aux=1;
         int i=1;
         FOR(i,0,9)
         {
             while(st[i]<dr[i])
             {
                 v[aux++]=(un)dequeue(i);
             }
         }
     }
 }

int main()
{
    citire();
    build_v();
    radixsort();
    afisare();
    return 0;
}