wind training

17/01/2024

wind training

Code
#include             <bits/stdc++.h>
#define pb           push_back
#define fi           first
#define se           second
#define int          long long 
#define pii          pair<long long, long long>
#define endl         "\n"
#define TIME         (1.0 * clock() / CLOCKS_PER_SEC)
#define MASK(i)      (1ll<<(i))
#define FILE(A)      freopen(A".INP", "r", stdin); freopen(A".OUT", "w", stdout)
#define BIT(Mask, i) (((Mask)>>(i))&1)

using namespace std;

const int N    = 1000000+5;
const int MOD  = 1e9+7;
const int INF  = 1e16 ;
const int Lim  = 1e9  ;
const int BASE = 31   ;

int n, a[N];
void solve() {
    cin >> n;
    for (int i=1;i <= n;i++) {
        cin >> a[i];
    }
    int id = 0;
    for (int i=n-1;i >= 1;i--) {
        if (a[i] < a[i+1]) {
            id = i;
            break;
        }
    }

    if (!id) {
        cout << -1;
        return;
    }
    int left=a[id];
    bool swapped=false;
    for (int i=id+1;i <= n;i++) {
        if (left < a[i]) {
            if(!swapped) {
                swapped=true;
                swap(a[id], a[i]);
            }
            else if (a[i] < a[id])
                swap(a[id], a[i]);
        }
    }
    sort(a+id+1, a+n+1);
    for (int i=1;i <= n;i++)
        cout << a[i];
    return;
}

signed main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

    int t; cin >> t;
    while(t--) {
    solve();
    cout << endl;
    }    
    
    cerr << "Time elapsed: " << TIME << "s.\n";
    return 0;
}

// Nguyen Minh Hieu - 2008 - Gia Lai.
// https://hieuhfgr.pythonanywhere.com/
// .-.. --- ...- . .-.. -.--