Problem: 2050D - Digital string maximization
Language: C++17 (GCC 7-32)

Previous submission (verdict: Time limit exceeded on test 6):
#include <algorithm>

#include <array>

#include <bitset>

#include <cassert>

#include <chrono>

#include <climits>

#include <cmath>

#include <complex>

#include <cstring>

#include <functional>

#include <iomanip>

#include <iostream>

#include <map>

#include <numeric>

#include <queue>

#include <random>

#include <set>

#include <vector>

using namespace std;



#define ll long long

#define forn(i, s, n) for (int i = s; i < n; i++)

#define bforn(i, s) for (int i = s; i >= 0; i--)

// for pairs

#define s second

#define f first 

const int M = 2e5 + 1;

const char nl = '\n';



void solve(){

    string s; cin >> s;

    int n = s.size(); 



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

        int ind = -1;

        for (int j = i + 1; j < min(n, j + 9); j++) {

            int sj = s[j] - '0';

            int si = s[i] - '0';

            if (sj - (j - i) > si) {

                if (ind == -1) ind = j;

                else if (s[j] - (j - i) > s[ind] - (ind - i)) ind = j;

            }

        }

        //cout << "ind: " << ind << nl;

        if (ind == -1) continue;

        while (ind > i) {

            char temp = s[ind - 1];

            s[ind - 1] = s[ind] - 1;

            s[ind] = temp;

            ind--;

        }

        //cout << s << nl;

    }

    cout << s << nl;

}

// if you are unsure of how to do a problem by hand

// the solution is most likely somewhat brute force (i.e binsearch)

// if input is limited to 0-9 or a-z, usually you will loop through all combinations

// of letters or numbers

// constructive problems : start with 1 when constructing

// :%y+ to copy all lines

// rearrange math expressions

// difference array? (a[i] - b[i])

// debugging : reread the problem statement-every word is important!

// debugging : print important variables!

// think brute force sol first

int main(){

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    cout.tie(nullptr);

    int t; cin >> t;

    for (int tt = 0; tt < t; tt++) solve();

}







Submission (verdict: Accepted):
#include <algorithm>

#include <array>

#include <bitset>

#include <cassert>

#include <chrono>

#include <climits>

#include <cmath>

#include <complex>

#include <cstring>

#include <functional>

#include <iomanip>

#include <iostream>

#include <map>

#include <numeric>

#include <queue>

#include <random>

#include <set>

#include <vector>

using namespace std;



#define ll long long

#define forn(i, s, n) for (int i = s; i < n; i++)

#define bforn(i, s) for (int i = s; i >= 0; i--)

// for pairs

#define s second

#define f first 

const int M = 2e5 + 1;

const char nl = '\n';



void solve(){

    string s; cin >> s;

    int n = s.size(); 



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

        int ind = -1;

        for (int j = i + 1; j < min(n, i + 10); j++) {

            int sj = s[j] - '0';

            int si = s[i] - '0';

            if (sj - (j - i) > si) {

                if (ind == -1) ind = j;

                else if (s[j] - (j - i) > s[ind] - (ind - i)) ind = j;

            }

        }

        //cout << "ind: " << ind << nl;

        if (ind == -1) continue;

        while (ind > i) {

            char temp = s[ind - 1];

            s[ind - 1] = s[ind] - 1;

            s[ind] = temp;

            ind--;

        }

        //cout << s << nl;

    }

    cout << s << nl;

}

// if you are unsure of how to do a problem by hand

// the solution is most likely somewhat brute force (i.e binsearch)

// if input is limited to 0-9 or a-z, usually you will loop through all combinations

// of letters or numbers

// constructive problems : start with 1 when constructing

// :%y+ to copy all lines

// rearrange math expressions

// difference array? (a[i] - b[i])

// debugging : reread the problem statement-every word is important!

// debugging : print important variables!

// think brute force sol first

int main(){

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    cout.tie(nullptr);

    int t; cin >> t;

    for (int tt = 0; tt < t; tt++) solve();

}