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

Previous submission (verdict: Wrong answer on test 4):
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
class list
{
public:
    int val;
    list* next=nullptr;
};

int main() {
    int n = 0;
    cin >> n;
    vector<int> de(n);
    vector<int> pl(n+1);
    list* head=new list;
    
    head->next = new list;
    list* now = head->next;
    now->val = 1;
    vector<pair<int,int>> im;
    for (int i = 0; i < n;  ++i)
    {
        cin >> de[i];
    }
    for (int i = 0; i < n; ++i)
    {
        pl[de[i]] = i ;
    }

    for (int i = 2; i <= n; ++i)
    {
        int first=0;
        list* now = head->next;
        while (now != nullptr)
        {
            stack<int> aa;
                if (pl[now->val] > pl[i])
                {
                    {
                    pair<int, int> ppp(i, now->val);
                   
                    im.push_back(ppp);
                    }
                    if (now->next == nullptr)
                    {
                        now->next = new list;
                        now->next->val = i;
                        break;
                    }
                }
            else
            {
                pair<int, int> ppp(i, now->val);
                aa.push(now->val);
                im.push_back(ppp);
                if (first == 0)
                {
                    list* nxt = new list;
                    nxt->val = now->val;
                    nxt->next = now->next;
                    now->val = i;
                    now->next = nxt;
                    first = 1;
                }
                now = now->next;
                while (now->next != nullptr)
                {
                    aa.push(now->val);
                    pair<int, int> ppp(i, now->val);
                    im.push_back(ppp);
                    now = now->next;
                }
                while (!aa.empty()) 
                {
                    pair<int, int> ppp(aa.top(), i); im.push_back(ppp);
                    aa.pop();
                }
            }
                if (now != nullptr)
                { 
                    {
                        now = now->next;
                    }
                }
        }
    }
    cout << im.size() << endl;
    for (auto a : im)
    {
        cout << a.first << " " << a.second << endl;
    }
    //cout << n * (n - 1);
    return 0;
 }


Submission (verdict: Wrong answer on test 4):
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
class list
{
public:
    int val;
    list* next=nullptr;
};

int main() {
    int n = 0;
    cin >> n;
    vector<int> de(n);
    vector<int> pl(n+1);
    list* head=new list;
    
    head->next = new list;
    list* now = head->next;
    now->val = 1;
    vector<pair<int,int>> im;
    for (int i = 0; i < n;  ++i)
    {
        cin >> de[i];
    }
    for (int i = 0; i < n; ++i)
    {
        pl[de[i]] = i ;
    }

    for (int i = 2; i <= n; ++i)
    {
        int first=0;
        list* now = head->next;
        while (now != nullptr)
        {
            stack<int> aa;
                if (pl[now->val] > pl[i])
                {
                    {
                    pair<int, int> ppp(i, now->val);
                   
                    im.push_back(ppp);
                    }
                    if (now->next == nullptr)
                    {
                        now->next = new list;
                        now->next->val = i;
                        break;
                    }
                }
            else
            {
                pair<int, int> ppp(i, now->val);
                aa.push(now->val);
                im.push_back(ppp);
                if (first == 0)
                {
                    list* nxt = new list;
                    nxt->val = now->val;
                    nxt->next = now->next;
                    now->val = i;
                    now->next = nxt;
                    first = 1;
                }
                now = now->next;
                while (now->next != nullptr)
                {
                    aa.push(now->val);
                    pair<int, int> ppp(i, now->val);
                    im.push_back(ppp);
                    now = now->next;
                }
                while (!aa.empty()) 
                {
                    pair<int, int> ppp(aa.top(), i); im.push_back(ppp);
                    aa.pop();
                }
            }
                if (now != nullptr)
                { 
                    {
                        now = now->next;
                    }
                }
        }
    }
    cout << im.size() << endl;
    for (auto a : im)
    {
        cout << a.first << " " << a.second << endl;
    }
  
    return 0;
 }