Cod sursa(job #2509055)

Utilizator SahMatCodrea Andrei SahMat Data 13 decembrie 2019 18:48:07
Problema Radix Sort Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.42 kb
#include <fstream>
#include <iostream>
#include <queue>
#include <cstring>

using namespace std;

int n, a, b, c, i, j, v[10000001], w[10000001], nrcif, maxi, p10, p, cif, cc[10], l[10];
long long t;
ifstream fin("radixsort.in");
ofstream fout("radixsort.out");

void repartizare (int a[], int b[]) {
  int d, c;
  memset (cc, 0, sizeof cc);
  for (i = 1; i <= n; i++) {
    c = a[i] / p10 % 10;
    cc[c]++; // contor pentru fiecare cifra
  }
  l[0] = 1;
  for (cif = 1; cif <= 9; cif++)
    l[cif] = l[cif - 1] + cc[cif - 1];
  for (i = 1; i <= n; i++) {
    c = a[i] / p10 % 10;
    d = l[c]++; // Destinatia urmatoare va fi mai mare cu 1.
    b[d] = a[i];
  }
}

int main() {
  fin >> n >> a >> b >> c;
  v[1] = b;
  for (i = 2; i <= n; i++) {
    t = (1LL * a * v[i - 1] + b) % c;
    v[i] = t;
    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++) {
    if (p % 2 == 1)
      repartizare(v, w);
    else
      repartizare(w, v);
    p10 *= 10; //inaintam cu pozitia cifrei pe care o vrem
  }
  //for (i = 1; i <= n; i += 10)
  if (nrcif % 2 == 1)
    for (i = 1; i <= n; i++)
      fout << w[i] << ' '; // se cere sa afisam elemente din 10 in 10
  else
    for (i = 1; i <= n; i += 10)
      fout << v[i] << ' '; // se cere sa afisam elemente din 10 in 10
  return 0;
}