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

Previous submission (verdict: Time limit exceeded on test 6):
#include <bits/stdc++.h>

using namespace std;

using ll = long long ; using ld = long double ;

constexpr ll mod = 1e9 + 7 ;

#define reverse(res) reverse(res.begin(),res.end());

#define all(x) (x).begin(), (x).end()

#define rall(x) (x).rbegin(), (x).rend()

#define umap unordered_map

#define uset unordered_set

#define pb push_back

#define eb emplace_back

#define pob pop_back

#define pii pair<int,int>

#define ff first

#define ss second

#define lb lower_bound

#define vi vector<int>

#define int long long

#define ub upper_bound

#define contain(map, i) (map.find(i) == map.end())

#define maxele max_element

#define minele min_element

#define len(s) (s).length()

#define nl cout<<endl

#define rep(i, a, b) for(int i = a; i < b; i++)

#define trav(it , strt , end) for(auto it = strt; it != end ; it++ )

/*

**************************************************************************************************

**************************************************************************************************

***        **************

***        **************

***  ***   ***   ***

***  ***   ***   ***

***        ***

*************************

*************************

***        ***

***   ***   ***  ***

***   ***   ***  ***

**************        ***

**************        ***

*************************************************************************************************

**************************************************************************************************

*/

/*ll gcd(ll a ,ll b){

if(b==0)return a;

return gcd(b,a%b);

}

inttobin(int n){

string ans='';

while(n!=0){

if(n%2==1)ans+k='1';

else ans+='0';

n=n/2;

}

reverse(ans);

cout<<ans<<endl;

}

int bintodec(string s){

int n=s.size();

for(int i=n-1;i>=0;i--){

if(s[i]=='1')num+=pow(2,i);

}

return num;

}

long long fastExpo(int a, int b,int m ) {

long long  res = 1;

while(b > 0) {

if(b&1) {

res = (res)%m*(a)%m;

}

b = b >> 1;

a = ((a)%m *( a)%m)%m;

}

return res;

}

bool checkithsetbit(int n,int i){

return (n&(1<<i)!=0);

}

int setithbit(int n,int i){

return (n|(1<<i));

}

int clearithbit(int n,int i){

return(n&(~(1<<i)));

}

int toggleithbit(int n,int i){

return (n^(1<<i));

}

int addm(int a, int b)

{ return ((a % mod) + (b % mod)) % mod;}

int subtm(int a, int b)

{ return ((a % mod) - (b % mod) + mod) % mod;}

int mul(int a, int b)

{ return ((a % mod) * (b % mod)) % mod; }

int modInverse(int a)

{return fastpow(a, mod - 2);}

int divm(int a, int b)

{    return mul(a, modInverse(b));}*/

/************************************************/

void solve(){

string s;

cin>>s;

int n=s.size();

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

// cout<<i<<" ";

int j=i;

int maxind=i;

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

// cout<<maxi<<" "<<maxind<<" ";

while(j<n){

// cout<<(s[j]-'0'-(j-i))<<" ";

if((s[j]-'0'-(j-i))>maxi){

maxi=s[j]-'0'-(j-i);

maxind=j;

}





j++;



}

// cout<<maxind<<" ";

if (maxind != i) {

char temp = s[maxind]-(maxind-i);





for (int j = maxind; j > i; j--) {

s[j] = s[j - 1];

}



s[i] = temp;

}

// cout<<s<<"\n";



}

cout<<s<<"\n";



return;

}



signed main() {

ios_base::sync_with_stdio(false);

cin.tie(); cout.tie(0);

#ifndef ONLINE_JUDGE

freopen("input.txt", "r", stdin);

freopen("output.txt", "w", stdout);

#endif

int t;

cin>>t;

while(t--){

solve();

}

return 0;

}

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

using namespace std;

using ll = long long ; using ld = long double ;

constexpr ll mod = 1e9 + 7 ;

#define reverse(res) reverse(res.begin(),res.end());

#define all(x) (x).begin(), (x).end()

#define rall(x) (x).rbegin(), (x).rend()

#define umap unordered_map

#define uset unordered_set

#define pb push_back

#define eb emplace_back

#define pob pop_back

#define pii pair<int,int>

#define ff first

#define ss second

#define lb lower_bound

#define vi vector<int>

#define int long long

#define ub upper_bound

#define contain(map, i) (map.find(i) == map.end())

#define maxele max_element

#define minele min_element

#define len(s) (s).length()

#define nl cout<<endl

#define rep(i, a, b) for(int i = a; i < b; i++)

#define trav(it , strt , end) for(auto it = strt; it != end ; it++ )

/*

**************************************************************************************************

**************************************************************************************************

***        **************

***        **************

***  ***   ***   ***

***  ***   ***   ***

***        ***

*************************

*************************

***        ***

***   ***   ***  ***

***   ***   ***  ***

**************        ***

**************        ***

*************************************************************************************************

**************************************************************************************************

*/

/*ll gcd(ll a ,ll b){

if(b==0)return a;

return gcd(b,a%b);

}

inttobin(int n){

string ans='';

while(n!=0){

if(n%2==1)ans+='1';

else ans+='0';

n=n/2;

}

reverse(ans);

cout<<ans<<endl;

}

int bintodec(string s){

int n=s.size();

for(int i=n-1;i>=0;i--){

if(s[i]=='1')num+=pow(2,i);

}

return num;

}

long long fastExpo(int a, int b,int m ) {

long long  res = 1;

while(b > 0) {

if(b&1) {

res = (res)%m*(a)%m;

}

b = b >> 1;

a = ((a)%m *( a)%m)%m;

}

return res;

}

bool checkithsetbit(int n,int i){

return (n&(1<<i)!=0);

}

int setithbit(int n,int i){

return (n|(1<<i));

}

int clearithbit(int n,int i){

return(n&(~(1<<i)));

}

int toggleithbit(int n,int i){

return (n^(1<<i));

}

int addm(int a, int b)

{ return ((a % mod) + (b % mod)) % mod;}

int subtm(int a, int b)

{ return ((a % mod) - (b % mod) + mod) % mod;}

int mul(int a, int b)

{ return ((a % mod) * (b % mod)) % mod; }

int modInverse(int a)

{return fastpow(a, mod - 2);}

int divm(int a, int b)

{    return mul(a, modInverse(b));}*/

/************************************************/

void solve(){

string s;

cin>>s;

int n=s.size();

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

// cout<<i<<" ";

int j=i;

int maxind=i;

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

// cout<<maxi<<" "<<maxind<<" ";

while(j<min(i+10,n)){

// cout<<(s[j]-'0'-(j-i))<<" ";

if((s[j]-'0'-(j-i))>maxi){

maxi=s[j]-'0'-(j-i);

maxind=j;

}





j++;



}

// cout<<maxind<<" ";

if (maxind != i) {

char temp = s[maxind]-(maxind-i);





for (int j = maxind; j > i; j--) {

s[j] = s[j - 1];

}



s[i] = temp;

}

// cout<<s<<"\n";



}

cout<<s<<"\n";



return;

}



signed main() {

ios_base::sync_with_stdio(false);

cin.tie(); cout.tie(0);

#ifndef ONLINE_JUDGE

freopen("input.txt", "r", stdin);

freopen("output.txt", "w", stdout);

#endif

int t;

cin>>t;

while(t--){

solve();

}

return 0;

}