Problem: 2052A - Adrenaline Rush
Language: java

Solution 1 (runtime: 593 ms):
  
/* package codechef; // don't place package name! */

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

public class Main {

    //------------------------------------------main--------------------------------------------------//
    public static void main(String[] args) throws java.lang.Exception {
        OutputStream outputStream = System.out;
        PrintWriter out = new PrintWriter(outputStream);
        FastReader sc = new FastReader();
        Task s = new Task(sc, out);
        s.solve();
        out.close();
        // your code goes here"
    }
}

class FastReader {

    BufferedReader br;
    StringTokenizer st;

    public FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

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

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

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

    String nextLine() {
        String str = "";
        try {
            str = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }

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

    public long[] readLongArray(int n) throws IOException {
        long[] arr = new long[n];
        for (int i = 0; i < n; i++) {
            arr[i] = nextLong();
        }
        return arr;
    }
}

class Task {

    PrintWriter out;
    FastReader sc;

    Task(FastReader sc, PrintWriter out) {
        this.sc = sc;
        this.out = out;
    }
    //----------------------------------------------inputfunction------------------------------------------//

    String st() {
        return sc.nextLine();
    }

    int i() {
        return sc.nextInt();
    }

    long l() {
        return sc.nextLong();
    }

    double d() {
        return sc.nextDouble();
    }

    //---------------------------------------------absolute----------------------------------------------//
    public long abs(long a) {
        return a < 0 ? -a : a;
    }

    public int abs(int a) {
        return a < 0 ? -a : a;
    }

    //-------------------------------------------max--------------------------------------------------//
    public int max(int a, int b) {
        return a >= b ? a : b;
    }

    public long max(long a, long b) {
        return a >= b ? a : b;
    }

    public int max(int a, int b, int c) {
        return Math.max(a, Math.max(b, c));
    }

    public long max(long a, long b, long c) {
        return Math.max(a, Math.max(b, c));
    }

    //------------------------------------------min---------------------------------------------------//
    public int min(int a, int b) {
        return a <= b ? a : b;
    }

    public long min(long a, long b) {
        return a <= b ? a : b;
    }

    public int min(int a, int b, int c) {
        return Math.min(a, Math.min(b, c));
    }

    public long min(long a, long b, long c) {
        return Math.min(a, Math.min(b, c));
    }

    //----------------------------------------sortingfunction---------------------------------------------//
    private void sort(int[] arr) {
        int n = arr.length;
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(arr[i]);
        }
        Collections.sort(list);
        for (int i = 0; i < n; i++) {
            arr[i] = list.get(i);
        }
    }

    private void sort(char[] arr) {
        int n = arr.length;
        List<Character> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(arr[i]);
        }
        Collections.sort(list);
        for (int i = 0; i < n; i++) {
            arr[i] = list.get(i);
        }
    }

    private void rsort(char[] arr) {
        int n = arr.length;
        List<Character> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(arr[i]);
        }
        Collections.sort(list, Collections.reverseOrder());
        for (int i = 0; i < n; i++) {
            arr[i] = list.get(i);
        }
    }

    private void rsort(int[] arr) {
        int n = arr.length;
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(arr[i]);
        }
        Collections.sort(list, Collections.reverseOrder());
        for (int i = 0; i < n; i++) {
            arr[i] = list.get(i);
        }
    }

    private void sort(long[] arr) {
        int n = arr.length;
        List<Long> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(arr[i]);
        }
        Collections.sort(list);
        for (int i = 0; i < n; i++) {
            arr[i] = list.get(i);
        }
    }

    private void rsort(long[] arr) {
        int n = arr.length;
        List<Long> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(arr[i]);
        }
        Collections.sort(list, Collections.reverseOrder());
        for (int i = 0; i < n; i++) {
            arr[i] = list.get(i);
        }
    }
    //---------------------------------------power----------------------------------------------------//

    public long pow(long a, int b) {
        long ans = 1L;
        while (b > 0) {
            if ((b & 1) > 0) {
                ans *= a;
            }
            a *= a;
            b >>= 1;
        }
        return b;
    }
    //----------------------------------------binary_exponentiation-----------------------------------//

    public long exp(long a, long b, long mod) {
        long ans = 1L;
        while (b > 0) {
            if ((b & 1) > 0) {
                ans = (ans * a) % mod;
            }
            a = (a * a) % mod;
            b >>= 1;
        }
        return ans;
    }

    //---------------------------------------helpingmethods-------------------------------------------//
    //---------------------------------------main-----------------------------------------------------//
    int t, n;

    public void solve() throws IOException {
        int n = i();
        int[] pos = new int[n+1], a = new int[n+1];
        for(int i = 1;i<=n;i++) pos[a[i] = i()] = i;
        List<int[]> l = new ArrayList<>();
        for(int i = n;i>=1;i--){
            for(int j = a[i]-1;j>=1;j--) if(pos[j]<pos[a[i]]){
                l.add(new int[]{a[i], j});
            }
            for(int j = 1;j<=a[i]-1;j++) if(pos[j]<pos[a[i]]){
                l.add(new int[]{j, a[i]});
            }
            for(int j = a[i]+1;j<=n;j++){
                if(pos[j]<pos[a[i]]){
                    l.add(new int[]{j,a[i]});
                } 
            }
        }
        out.print(l.size()+"\n");
        for(int[] e: l) out.print(e[0]+" "+e[1]+"\n");
    }
}


Solution 2 (runtime: 2171 ms):
import java.io.*;
import java.util.*;

public class Main
{
    public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    public static  BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    public static StringTokenizer tokenizer;
    public static void read() throws IOException {
        tokenizer = new StringTokenizer(br.readLine());
    }
    public static int Int() throws IOException {
        if (tokenizer == null || !tokenizer.hasMoreTokens()) read();
        return Integer.parseInt(tokenizer.nextToken());
    }
    public static long Long() throws IOException {
        if (tokenizer == null || !tokenizer.hasMoreTokens()) read();
        return Long.parseLong(tokenizer.nextToken());
    }
    public static String Str() throws IOException {
        return br.readLine();
    }

    public static Character Char() throws IOException {
        return (char)br.read();
    }

    public static long[] read(int n) throws IOException {
        long[] a = new long[n];
        for (int i = 0; i < n; i++) {
            a[i] = Long() - 1;
        }
        return a;
    }

    //-----------------------------------------------

    public static long MOD = (long) 1e9 + 7;
    public static long quickPow(long pow, long num, long MOD) {
        if (pow == 0) {
            return 1;
        }
        if (pow == 1) {
            return num % MOD;
        }
        long half = quickPow(pow / 2, num, MOD);
        long twoHalf = (half * half) % MOD;
        return (twoHalf * quickPow(pow % 2, num, MOD)) % MOD;
    }

    public static void remove(TreeMap<Long, Long> map, long key) {
        map.put(key, map.get(key) - 1);
        if (map.get(key) == 0) map.remove(key);
    }

    public static void remove(HashMap<Long, Long> map, long key) {
        map.put(key, map.get(key) - 1);
        if (map.get(key) == 0) map.remove(key);
    }

    public static void add(TreeMap<Long, Long> map, long key) {
        map.put(key, map.getOrDefault(key, 0L) + 1);
    }

    public static void add(HashMap<Long, Long> map, long key) {
        map.put(key, map.getOrDefault(key, 0L) + 1);
    }




    public static void main(String[] args) throws IOException {
        int T = 1;
        while (T > 0) {
            T--;
            int n = Int();
            int[] a = new int[n];
            int[] start = new int[n];
            HashMap<Integer, Integer> pos = new HashMap<>();
            for (int i = 0; i < n; i++) {
                a[i] = Int();
                start[i] = i + 1;
                pos.put(i + 1, i);
            }
            ArrayList<int[]> res = new ArrayList<>();
            for (int i = n - 1; i >= 0; i--) {
                int position = pos.get(a[i]);
                for (int j = position; j > 0; j--) {
                    res.add(new int[] {start[j], start[j - 1]});
                    pos.put(start[j], j - 1);
                    pos.put(start[j - 1], j);
                    int tmp = start[j];
                    start[j] = start[j - 1];
                    start[j - 1] = tmp;
                }
            }
            System.out.println(res.size());
            for (int i = 0; i < res.size(); i++) {
                System.out.println(res.get(i)[0] + " " + res.get(i)[1]);
            }
        }
    }
}