Advertisement
STANAANDREY

miub2021: sort

Feb 5th, 2022
921
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <climits>
  4. using namespace std;
  5. constexpr int NMAX = 1e4 + 3;
  6. int n, a[NMAX], b[NMAX];
  7. bool ok = true;
  8.  
  9. void read() {
  10.     cin >> n;
  11.     for (int i = 1; i <= n; i++) {
  12.         cin >> a[i];
  13.     }
  14. }
  15.  
  16. int getCircPerm(const int x) {
  17.     int fiDig = x, nrDigs = 1;
  18.     while (fiDig > 9) {
  19.         fiDig /= 10;
  20.         nrDigs++;
  21.     }
  22.     int r = 0;
  23.     int pow10 = pow(10, nrDigs - 1);
  24.     r = x % pow10;
  25.     r = r * 10 + fiDig;
  26.     return r;
  27. }
  28.  
  29. void solve() {
  30.     for (int i = 1; i <= n && ok; i++) {
  31.         int mini = INT_MAX, curr = a[i];
  32.         do {
  33.             if (mini > curr && curr > a[i - 1]) {
  34.                 mini = curr;
  35.             }
  36.             curr = getCircPerm(curr);
  37.         } while(curr != a[i]);
  38.         if (mini == INT_MAX) {
  39.             ok = false;
  40.         } else {
  41.             b[i] = mini;
  42.         }
  43.     }
  44. }
  45.  
  46. void write() {
  47.     cout << (ok ? "DA" : "NU") << endl;
  48.     if (ok) {
  49.         for (int i = 1; i <= n; i++) {
  50.             cout << b[i] << ' ';
  51.         }
  52.     }
  53. }
  54.  
  55. int main() {
  56.     //freopen("sort.in", "r", stdin);
  57.     //freopen("sort.out", "w", stdout);
  58.  
  59.     read();
  60.     solve();
  61.     write();
  62.     return 0;
  63. }
  64.  
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement