Problem: 2052A - Adrenaline Rush
Language: Rust 2021

Previous submission (verdict: Wrong answer on test 1):
use std::io::{self, prelude::*};
use std::ops::Index;
use std::str;
 
struct Scanner<R> {
    reader: R,
    buf_str: Vec<u8>,
    buf_iter: str::SplitWhitespace<'static>,
}

impl<R: BufRead> Scanner<R> {
    fn new(reader: R) -> Self {
        Self { reader, buf_str: vec![], buf_iter: "".split_whitespace() }
    }
    fn token<T: str::FromStr>(&mut self) -> T {
        loop {
            if let Some(token) = self.buf_iter.next() {
                return token.parse().ok().expect("Failed parse");
            }
            self.buf_str.clear();
            self.reader.read_until(b'\n', &mut self.buf_str).expect("Failed read");
            self.buf_iter = unsafe {
                let slice = str::from_utf8_unchecked(&self.buf_str);
                std::mem::transmute(slice.split_whitespace())
            }
        }
    }
}

#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
struct Car(usize);

struct RacetrackConfiguration {
    car_order: Vec<Car>,
    overtakes_so_far: Vec<(Car, Car)>,
}

impl RacetrackConfiguration {
    fn new(n: usize) -> Self {
        let mut car_order = Vec::new();

        for i in 1..=n {
            car_order.push(Car(i));
        }
        Self {
            car_order,
            overtakes_so_far: vec![],
        }
    }

    fn overtake_next(&mut self, index: usize) {
        self.overtakes_so_far.push((self.car_order[index+1], self.car_order[index]));
        self.car_order.swap(index, index+1);
    }
}

impl Index<usize> for RacetrackConfiguration {
    type Output = Car;

    fn index(&self, index: usize) -> &Self::Output {
        &self.car_order[index]
    }
}

fn solve<R: BufRead, W: Write>(scan: &mut Scanner<R>, w: &mut W) {
    let n = scan.token::<usize>();
    let mut finish_position = vec![];

    for _ in 0..n {
        finish_position.push(scan.token::<usize>())
    }

    let mut current_configuration = RacetrackConfiguration::new(n);

    for car_num in 1..=n {
        if n == 1 {
            break;
        }
        let car = Car(car_num);
        let mut current_pos = 0;
        let mut next_pos = 1;
        let mut next_car = current_configuration[1];
        while next_pos < n 
            && (next_car > car || finish_position[car.0 - 1] > finish_position[next_car.0 - 1]) {
                current_configuration.overtake_next(current_pos);
                current_pos += 1;
                next_pos += 1;
                if next_pos < n {
                    next_car = current_configuration[next_pos];
                }
        }
    }
    writeln!(w, "{}", current_configuration.overtakes_so_far.len()).ok();
    for (car1, car2) in current_configuration.overtakes_so_far {
        writeln!(w, "{} {}", car1.0, car2.0).ok();
    }
}
 
fn main() {
    let (stdin, stdout) = (io::stdin(), io::stdout());
    let mut scan = Scanner::new(stdin.lock());
    let mut out = io::BufWriter::new(stdout.lock());
    solve(&mut scan, &mut out);
}


Submission (verdict: Accepted):
use std::io::{self, prelude::*};
use std::ops::Index;
use std::str;
 
struct Scanner<R> {
    reader: R,
    buf_str: Vec<u8>,
    buf_iter: str::SplitWhitespace<'static>,
}

impl<R: BufRead> Scanner<R> {
    fn new(reader: R) -> Self {
        Self { reader, buf_str: vec![], buf_iter: "".split_whitespace() }
    }
    fn token<T: str::FromStr>(&mut self) -> T {
        loop {
            if let Some(token) = self.buf_iter.next() {
                return token.parse().ok().expect("Failed parse");
            }
            self.buf_str.clear();
            self.reader.read_until(b'\n', &mut self.buf_str).expect("Failed read");
            self.buf_iter = unsafe {
                let slice = str::from_utf8_unchecked(&self.buf_str);
                std::mem::transmute(slice.split_whitespace())
            }
        }
    }
}

#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
struct Car(usize);

struct RacetrackConfiguration {
    car_order: Vec<Car>,
    overtakes_so_far: Vec<(Car, Car)>,
}

impl RacetrackConfiguration {
    fn new(n: usize) -> Self {
        let mut car_order = Vec::new();

        for i in 1..=n {
            car_order.push(Car(i));
        }
        Self {
            car_order,
            overtakes_so_far: vec![],
        }
    }

    fn overtake_next(&mut self, index: usize) {
        self.overtakes_so_far.push((self.car_order[index], self.car_order[index - 1]));
        self.car_order.swap(index, index-1);
    }
}

impl Index<usize> for RacetrackConfiguration {
    type Output = Car;

    fn index(&self, index: usize) -> &Self::Output {
        &self.car_order[index]
    }
}

fn solve<R: BufRead, W: Write>(scan: &mut Scanner<R>, w: &mut W) {
    let n = scan.token::<usize>();
    let mut finish_position = vec![0; n];

    for i in 0..n {
        finish_position[scan.token::<usize>() - 1] = i;
    }

    let mut current_configuration = RacetrackConfiguration::new(n);

    for car_num in (1..=n).rev() {
        if n == 1 {
            break;
        }
        let car = Car(car_num);
        let mut current_pos = n - 1;
        let mut next_pos = n - 2;
        let mut next_car = current_configuration[next_pos];
        while current_pos > 0 && (next_car < car || finish_position[car.0 - 1] < finish_position[next_car.0 - 1]) {
                current_configuration.overtake_next(current_pos);
                current_pos -= 1;
                if next_pos > 0 {
                    next_pos -= 1;
                    next_car = current_configuration[next_pos];
                }
        }
    }
    writeln!(w, "{}", current_configuration.overtakes_so_far.len()).ok();
    for (car1, car2) in current_configuration.overtakes_so_far {
        writeln!(w, "{} {}", car1.0, car2.0).ok();
    }
}
 
fn main() {
    let (stdin, stdout) = (io::stdin(), io::stdout());
    let mut scan = Scanner::new(stdin.lock());
    let mut out = io::BufWriter::new(stdout.lock());
    solve(&mut scan, &mut out);
}