Problem: 2050D - Digital string maximization
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>

using namespace std;

#ifdef DEBUG

#include "debug.h"

#else

#define dbg(x...)

#endif

#define fastInOut ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

#define endl '\n'

#define mod 1000000007

#define pi 3.14159265358979323846

#define mem(a,b) memset(a, b, sizeof(a) )

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;

template <typename T>

using orderer_set = tree<T,  null_type ,  less <T>,  rb_tree_tag ,  tree_order_statistics_node_update>;

///cout << *x.find_by_order(k); // iterator to the k-th largest element

///cout << x.order_of_key (k); // number of items in a set that are strictly smaller then our item

typedef double dl;

typedef long long ll;

using ull = unsigned long long;

void solve()

{

       string s;

       cin >> s;

       int n = s.size ();

       char c = '8';

       for (int i = 1; i < n; i ++) {

         char ch = s[i - 1];

         ch ++;

         if (s[i - 1] == s[i] || s[i] == ch || ch > s[i]) continue;

         if (s[i - 1] < ch) {

            int l = i - 1;

            int r = i;

            while (l >= 0) {

                char x = s[l];

                if (x < c) x += 2;

                if (x > s[r]) break;

                s[r] --;

                swap (s[r], s[l]);

                l --;

                r --;

            }

         }

       }

       cout << s << endl;

}



int32_t main()

{

        fastInOut;

        //int t = 1;

        int t; cin >> t;

        for (int i = 1; i <= t; i ++) {

          ///cout << "Case " << i << ": ";

          solve();

        }

        return 0;

}



Submission (verdict: Accepted):
#include <bits/stdc++.h>

using namespace std;

#ifdef DEBUG

#include "debug.h"

#else

#define dbg(x...)

#endif

#define fastInOut ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

#define endl '\n'

#define mod 1000000007

#define pi 3.14159265358979323846

#define mem(a,b) memset(a, b, sizeof(a) )

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;

template <typename T>

using orderer_set = tree<T,  null_type ,  less <T>,  rb_tree_tag ,  tree_order_statistics_node_update>;

///cout << *x.find_by_order(k); // iterator to the k-th largest element

///cout << x.order_of_key (k); // number of items in a set that are strictly smaller then our item

typedef double dl;

typedef long long ll;

using ull = unsigned long long;

void solve()

{

       string s;

       cin >> s;

       int n = s.size ();

       char c = '8';

       for (int i = 1; i < n; i ++) {

         char ch = s[i - 1];

         ch ++;

         if (s[i - 1] == s[i] || s[i] == ch || ch > s[i]) continue;

         if (s[i - 1] < ch) {

            int l = i - 1;

            int r = i;

            while (l >= 0) {

                char x = s[l];

                x ++;

                if (x > s[r] || s[r] == x) break;

                s[r] --;

                swap (s[r], s[l]);

                l --;

                r --;

            }

         }

       }

       cout << s << endl;

}



int32_t main()

{

        fastInOut;

        //int t = 1;

        int t; cin >> t;

        for (int i = 1; i <= t; i ++) {

          ///cout << "Case " << i << ": ";

          solve();

        }

        return 0;

}