Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <climits>
- using namespace std;
- constexpr int NMAX = 1e4 + 3;
- int n, a[NMAX], b[NMAX];
- bool ok = true;
- void read() {
- cin >> n;
- for (int i = 1; i <= n; i++) {
- cin >> a[i];
- }
- }
- int getCircPerm(const int x) {
- int fiDig = x, nrDigs = 1;
- while (fiDig > 9) {
- fiDig /= 10;
- nrDigs++;
- }
- int r = 0;
- int pow10 = pow(10, nrDigs - 1);
- r = x % pow10;
- r = r * 10 + fiDig;
- return r;
- }
- void solve() {
- for (int i = 1; i <= n && ok; i++) {
- int mini = INT_MAX, curr = a[i];
- do {
- if (mini > curr && curr > a[i - 1]) {
- mini = curr;
- }
- curr = getCircPerm(curr);
- } while(curr != a[i]);
- if (mini == INT_MAX) {
- ok = false;
- } else {
- b[i] = mini;
- }
- }
- }
- void write() {
- cout << (ok ? "DA" : "NU") << endl;
- if (ok) {
- for (int i = 1; i <= n; i++) {
- cout << b[i] << ' ';
- }
- }
- }
- int main() {
- //freopen("sort.in", "r", stdin);
- //freopen("sort.out", "w", stdout);
- read();
- solve();
- write();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement