Cod sursa(job #2509048)

Utilizator SahMatCodrea Andrei SahMat Data 13 decembrie 2019 18:20:55
Problema Radix Sort Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.75 kb
#include <fstream>
#include <iostream>
#include <queue>

using namespace std;

int n, a, b, c, i, j, nrcif, maxi, p10, p, iq, cont, t;
int v[10000001];
queue<int> q[10];
ifstream fin("radixsort.in");
ofstream fout("radixsort.out");

int main() {
  fin >> n >> a >> b >> c;
  v[1] = b;
  for (i = 2; i <= n; i++) {
    v[i] = (1LL * a * v[i - 1] % c + b) % c;
    //v[i] = ((a * v[i - 1] + b) % c;
    if ( v[i] > maxi )
      maxi = v[i];
  }
  while (maxi > 0) {
    maxi /= 10;
    nrcif++;
  } //nr de cifre maxim din sir
  p10 = 1;
  for (p = 1; p <= nrcif; p++) {
    for (i = 1; i <= n; i++) { //pt fiecare element din vectorul v
      c = v[i] / p10 % 10; //determinam cifra unitatilor/zecilor/sutelor etc.
      q[c].push(v[i]); // si punem elementul v[i] la coada
    }
    i = 0;
    for (iq = 0; iq <= 9; iq++)  // Punem elementele in v.
      while (not q[iq].empty()) { //cat timp exista coada
        i++;
        v[i] = q[iq].front();
        q[iq].pop(); //eliminam coada, nu mai avem nevoie de ea
      }
    p10 *= 10; //inaintam cu pozitia cifrei pe care o vrem
  }
  for (i = 1; i <= n; i += 10)
    fout << v[i] << ' '; // se cere sa afisam elemente din 10 in 10
  return 0;
}

/*

849 770 67 347 201 618 66 495 13 45

   0   1   2   3   4   5   6   7   8   9
 770 201      13     495  66  67 618 849
                      45     347

770 201 13 495 45 66 67 347 618 849

   0   1   2   3   4   5   6   7   8   9
 201  13           45     66 770     495
     618          347     67
                  849

201 13 618 45 347 849 66 67 770 495

     0   1   2   3   4   5   6   7   8   9
    13     201 347 495     618 770 849
    45
    66
    67

13 45 66 67 201 347 495 618 770 849

O (n log n)

*/