Problem: 2052A - Adrenaline Rush
Language: C++23 (GCC 14-64, msys2)

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

using namespace std;

#define int long long int



bool present(int a, int b, const vector<vector<int>>& mat) {

    if (a > 1000 || a < 0 || mat[a].empty()) return false;

    return binary_search(mat[a].begin(), mat[a].end(), b);

}



signed main() {

    int t;

    t = 1;

    while (t--) {

        int n;

        cin >> n;

        vector<int>arr(n,0);

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

            cin >> arr[i];

        }



        vector<int> v(n,0);

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

            v[i-1]=i;

        }

        

        int ind = 1e9;

        map<int, int> mp;

        vector<pair<int, int>> ans;

        vector<vector<int>> mat(1001);



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

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

               if(arr[j]<arr[i]){

                   mat[arr[i]].push_back(arr[j]);

               }

           }

       }

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

            if (v[i] != arr[i]) {

                int ind = -1;

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

                    if (v[i] == arr[j]) {

                        ind=j;

                        break;

                    }

                }

                if (ind == -1) {

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

                        if (v[i] == arr[j]) {

                            ind=j;

                            break;

                        }

                    }

                    for (int k = i; k <= ind - 1; k++) {

                        ans.push_back({v[k+1], v[k]});

                      //  mat[v[k]].push_back(v[k + 1]);

                        swap(v[k], v[k + 1]);

                    }

                } else {

                    for (int k = i - 1; k >= ind; k--) {

                        ans.push_back({v[k], v[k+1]});

                       // mat[v[k + 1]].push_back(v[k]);

                        swap(v[k + 1], v[k]);

                    }

                }

            }

        }

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

            if (mat[i].size() > 0)

                sort(mat[i].begin(), mat[i].end());

        }

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

        //    cout<<" value of i"<<i<<endl;

        //    for(int ele:mat[i]){

        //        cout<<ele<<" ";

        ////    }

//cout<<endl;

      //  }



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

            vector<int> l;

            int element = v[i];

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

                if (!present(element, v[j], mat) && !present(v[j], element, mat)) {

                    l.push_back(v[j]);

                } else {

                    break;

                }

            }

            for (int x : l) {

                ans.push_back({element, x});

                //mat[element].push_back(x);

            }

            reverse(l.begin(), l.end());

            for (int x : l) {

                ans.push_back({x, element});

              //  mat[x].push_back(element);

            }

            l.clear();





        }



        cout << ans.size() << endl;

        for (auto p : ans) {

            cout << p.first << " " << p.second << endl;

        }

    }

}



Submission (verdict: Wrong answer on test 7):
#include <bits/stdc++.h>

using namespace std;

#define int long long int



// Function to check if `b` is present in `mat[a]` using binary search

bool present(int a, int b, const vector<vector<int>>& mat) {

    if (a > 1000 || a < 0 || mat[a].empty()) return false;

    return binary_search(mat[a].begin(), mat[a].end(), b);

}



signed main() {

    int t;

    t = 1;

    while (t--) {

        int n;

        cin >> n;

        vector<int>arr(n,0);

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

            cin >> arr[i];

        }



        vector<int> v(n,0);

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

            v[i-1]=i;

        }

        

        int ind = 1e9;

        map<int, int> mp;

        vector<pair<int, int>> ans;

        vector<vector<int>> mat(1001);



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

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

               if(arr[j]<arr[i]){

                   mat[arr[i]].push_back(arr[j]);

               }

           }

       }

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

            if (v[i] != arr[i]) {

                int ind = -1;

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

                    if (v[i] == arr[j]) {

                        ind=j;

                        break;

                    }

                }

                if (ind == -1) {

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

                        if (v[i] == arr[j]) {

                            ind=j;

                            break;

                        }

                    }

                    for (int k = i; k <= ind - 1; k++) {

                        ans.push_back({v[k+1], v[k]});

                      //  mat[v[k]].push_back(v[k + 1]);

                        swap(v[k], v[k + 1]);

                    }

                } else {

                    for (int k = i - 1; k >= ind; k--) {

                        ans.push_back({v[k+1], v[k]});

                       // mat[v[k + 1]].push_back(v[k]);

                        swap(v[k + 1], v[k]);

                    }

                }

            }

        }

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

            if (mat[i].size() > 0)

                sort(mat[i].begin(), mat[i].end());

        }

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

        //    cout<<" value of i"<<i<<endl;

        //    for(int ele:mat[i]){

        //        cout<<ele<<" ";

        ////    }

//cout<<endl;

      //  }



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

            vector<int> l;

            int element = v[i];

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

                if (!present(element, v[j], mat) && !present(v[j], element, mat)) {

                    l.push_back(v[j]);

                } else {

                    break;

                }

            }

            for (int x : l) {

                ans.push_back({element, x});

                //mat[element].push_back(x);

            }

            reverse(l.begin(), l.end());

            for (int x : l) {

                ans.push_back({x, element});

              //  mat[x].push_back(element);

            }

            l.clear();





        }



        cout << ans.size() << endl;

        for (auto p : ans) {

            cout << p.first << " " << p.second << endl;

        }

    }

}