Problem: 2052A - Adrenaline Rush
Language: java

Solution 1 (runtime: 968 ms):
//package Practice;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

public class AdrenalineRush {
    public static void swap(int[] arr, int l) {
        int temp = arr[l];
        arr[l] = arr[l+1];
        arr[l+1] = temp;
    }
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(System.out);
        int n = Integer.parseInt(br.readLine());
        int[] arr = new int[n];
        StringTokenizer st = new StringTokenizer(br.readLine());
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < n; i++) {
            arr[i] = Integer.parseInt(st.nextToken());
            map.put(arr[i], i);
         }
        if (n == 1) {
            pw.println("0");
            pw.close();
            return;
        }
        HashMap<Integer, Integer> map2 = new HashMap<>();
        int[] arr2 = new int[n];
        for (int i = 0; i < n; i++) {
            arr2[i] = i+1;
            map2.put(i+1, i);
        }
        ArrayList<String> moves= new ArrayList<>();
        for (int i = n-1; i >= 0; i--) {
            int curNum = arr[i];
            int startIdx = map2.get(curNum);
            int endIdx = map.get(curNum);
            // pass everything that starts in front
           // System.out.println("starting passing phase, arr2 is " + Arrays.toString(arr2) + " and curNum is " + curNum + " and start: " + startIdx);
            for (int j = startIdx-1; j >= 0; j--) {
                moves.add(arr2[j+1] + " " +  arr2[j]);
                swap(arr2, j);
                //System.out.println("passed 1, move was " + moves.getLast());
            }
            for (int j = 0; j < startIdx; j++) {
                map2.put(arr2[j], j);
            }
            startIdx = map2.get(curNum);
            // then get passed by everything that ends in front
            //System.out.println("at i="+i+", curNum is " + curNum + " and start:" + startIdx + " and end:" + endIdx);
            if (startIdx < endIdx) {
                //System.out.println("starting going back phase");
                while (startIdx < endIdx) {
                    //System.out.println("one move with num = " + curNum);
                    moves.add(arr2[startIdx+1] + " " + arr2[startIdx]);
                    int temp = arr2[startIdx];
                    map2.put(arr2[startIdx], map2.get(arr2[startIdx]) + 1);
                    map2.put(arr2[startIdx+1], map2.get(arr2[startIdx+1]) - 1);
                    arr2[startIdx] = arr2[startIdx+1];
                    arr2[startIdx+1] = temp;
                    startIdx++;
            //        pw.println("arr2:" + Arrays.toString(arr2) + " and last move was " + moves.getLast());
                }
            }
            for (int j = 0; j < startIdx; j++) {
                map2.put(arr2[j], j);
            }
          //  System.out.println("arr2 at the end of the process:" + Arrays.toString(arr2));
        }
        //System.out.println("arr2:" + Arrays.toString(arr2));
        /*HashSet<String> moveSet = new HashSet<>();
        moveSet.addAll(moves);
        for (int i = 0; i < n - 1; i++) {
            if (!moveSet.contains(arr2[i] + " " + arr2[i+1]) && !moveSet.contains(arr2[i+1] + " " + arr2[i])) {
                moves.add(arr2[i+1] + " " + arr2[i]);
                moves.add(arr2[i] + " " + arr2[i+1]);
            }
        }*/
        pw.println(moves.size());
        for (String move: moves) {
            pw.println(move);
        }
        // do extra stuff

        pw.close();
        br.close();
    }
}


Solution 2 (runtime: 1249 ms):

import java.io.*;
import java.util.*;

public class ComTemp2 {

    private static final long MINA = 1;
    private static final long MAXA = 1000000000000L;
    static BufferedReader reader;
    static StringTokenizer tokenizer;
    static PrintWriter writer;

    static int MOD = (int) (1e9 + 7);
    static final int MOD1 = 998244353;

    public static Scanner scanner = new Scanner(System.in);

    public static boolean flag1 = true, flag2 = true;

    public static void main(String[] args) throws IOException {
        reader = new BufferedReader(new InputStreamReader(System.in));
        writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        tokenizer = new StringTokenizer("");

//        int n = nextInt();
        int n = 1;
        while (n-- > 0) {
            solve();
        }

        writer.close();
    }


    static int fun(int a1, int a2, int a3, int a4, int a5) {
        int count = 0;
        if (a3 == a1 + a2) count++;
        if (a4 == a2 + a3) count++;
        if (a5 == a3 + a4) count++;
        return count;
    }

    public static void solve() throws IOException {
        int n = nextInt();
        long[] arr = nextLongArray(n);
        ArrayList<Pair<Long, Long>> list = new ArrayList<>();
        Pair<Long, Long>[] res = new Pair[n];
        for (int i = 0; i < n; i++) {
            res[i] = new Pair<>(arr[i], i + 1L);
        }
        for (int i = n; i > 0; i--) {
            int j = 0;
            while (arr[j] != i) {
                j++;
            }

            for (int k = j; k > 0; k--) {
                long tem = arr[k];
                arr[k] = arr[k - 1];
                arr[k - 1] = tem;
                list.add(new Pair<>(arr[k], arr[k - 1]));
            }

            for (int k = 0; k < i - 1; k++) {
                long tem = arr[k];
                arr[k] = arr[k + 1];
                arr[k + 1] = tem;
                list.add(new Pair<>(arr[k + 1], arr[k]));
            }
        }

        int s = list.size();
        writer.println(s);
        for (int i = s-1; i >= 0; i--) {
            writer.println(list.get(i).key + " " + list.get(i).value);
        }
    }

    public static void solv() throws IOException {
        int n = nextInt();
        for (int i = 0; i < 2; i++) {
            writer.print(1 + " ");
        }
        for (int i = 2; i < n - 1; i++) {
            writer.print(i + " ");
        }
        writer.println(1);
    }

    public static boolean isPowerLogarithmic(int x, int y) {
        if (x <= 0 || y <= 0 || y == 1) return false; // Edge cases

        double logResult = Math.log(x) / Math.log(y);
        return Math.abs(logResult - Math.round(logResult)) < 1e-10;
    }


/*
14
10100001000101
15
001110100100001
14
10100101111101

11
01100101101
14
01001111101001
 */

    private static int ask(int l, int r) throws IOException {
        if (r - l + 1 <= 1)
            return 0;
        writer.println(String.valueOf("? ") + String.valueOf(l) + " " + String.valueOf(r));
        writer.flush();
        return nextInt();
    }

    public static int leftmostBit(int n) {
        return (int) Math.pow(2, (int) (Math.log(n) / Math.log(2)));
    }

    public static int leftmostBit(long n) {
        return (int) Math.pow(2, (long) (Math.log(n) / Math.log(2)));
    }

    public static long gcdOfArray(int[] arr) {
        long result = arr[0];
        for (int i = 1; i < arr.length; i++) {
            result = gcd(result, arr[i]);
            if (result == 1)
                break; // Early exit if GCD is 1
        }
        return result;
    }

    static long gcd(long a, long b) {
        return b == 0 ? a : gcd(b, a % b);
    }

    public static List<Integer> findDivisors(int n) {
        List<Integer> divisors = new ArrayList<>();

        for (int i = 1; i <= Math.sqrt(n); i++) {
            if (n % i == 0) {
                divisors.add(i); // Add the divisor
                if (i != n / i) { // Avoid duplicate for perfect squares
                    divisors.add(n / i);
                }
            }
        }

        // Optional: Sort the divisors if needed
        divisors.sort(Integer::compareTo);
        return divisors;
    }

    public static long[] unique(long[] arr) {
        int n = arr.length;
        ArrayList<Long> l1 = new ArrayList<>();
        l1.add(arr[0]);
        for (int i = 1; i < n; i++) {
            if (arr[i] == arr[i - 1]) {
                continue;
            } else {
                l1.add(arr[i]);
            }
        }
        n = l1.size();
        long[] tem = new long[n];
        for (int i = 0; i < n; i++) {
            tem[i] = l1.get(i);
        }
        return tem;
    }

    public static void reverseArray(long[] array) {
        int start = 0, end = array.length - 1;
        while (start < end) {
            long temp = array[start];
            array[start] = array[end];
            array[end] = temp;
            start++;
            end--;
        }
    }

    public static <T> void printArr(int[] arr) {
        for (int e : arr) {
            writer.print(e + " ");
        }
        writer.println();
    }

    public static void printArr(long[] arr) {
        for (long e : arr) {
            writer.print(e + " ");
        }
        writer.println();
    }

    private static int lowerBound(long[] arr, long target, int start, int end) {
        int left = start, right = end;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (arr[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return left;
    }

    // Binary search for the last index where arr[index] <= target
    private static int upperBound(long[] arr, long target, int start, int end) {
        int left = start, right = end;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (arr[mid] <= target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
//        if (arr[right]>target)return -1;
        return right;
    }

    public static class Pair<K, V> {
        private K key;
        private V value;

        public Pair(K key, V value) {
            this.key = key;
            this.value = value;
        }

        public K getKey() {
            return key;
        }

        public void setKey(K key) {
            this.key = key;
        }

        public V getValue() {
            return value;
        }

        public void setValue(V value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return "Pair{" + "key=" + key + ", value=" + value + '}';
        }
    }

    static String next() throws IOException {
        while (!tokenizer.hasMoreTokens()) {
            tokenizer = new StringTokenizer(reader.readLine());
        }
        return tokenizer.nextToken();
    }

    static String nextLine() throws IOException {
        return reader.readLine();
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    static int[] nextIntArray(int n) throws IOException {
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = nextInt();
        }
        return arr;
    }

    // Function to read the next array of longs
    static long[] nextLongArray(int n) throws IOException {
        long[] arr = new long[n];
        for (int i = 0; i < n; i++) {
            arr[i] = nextLong();
        }
        return arr;
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }
}