From b6fd714726003488ecdd53074edd51bb91a7106c Mon Sep 17 00:00:00 2001 From: yenru0 Date: Mon, 19 May 2025 09:20:19 +0900 Subject: [PATCH] complete 33027.py 31159.rs --- storage/zeta/py/completed/33027.py | 28 +++++++++++ storage/zeta/rs/31159.rs | 74 ------------------------------ storage/zeta/rs/completed/31159.rs | 41 +++++++++++++++++ 3 files changed, 69 insertions(+), 74 deletions(-) create mode 100644 storage/zeta/py/completed/33027.py delete mode 100644 storage/zeta/rs/31159.rs create mode 100644 storage/zeta/rs/completed/31159.rs diff --git a/storage/zeta/py/completed/33027.py b/storage/zeta/py/completed/33027.py new file mode 100644 index 0000000..a5adbae --- /dev/null +++ b/storage/zeta/py/completed/33027.py @@ -0,0 +1,28 @@ +class AdrenalineRushSolver: + def __init__(self, n: int, final_order: list[int]): + self.n = n + self.final_order = final_order + + def solve(self) -> list[tuple[int, int]]: + overtaken_history = [] + for i in range(self.n): + ro = self.final_order.index(i) + for j in range(ro + 1, self.n): + overtaken_history.append((self.final_order[j], i)) + for j in range(self.n - 1, ro, -1): + overtaken_history.append((i, self.final_order[j])) + for j in range(ro, i, -1): + overtaken_history.append((i, self.final_order[j - 1])) + self.final_order[j], self.final_order[j - 1] = self.final_order[j - 1], self.final_order[j] + + return overtaken_history[::-1] + + +if __name__ == '__main__': + n = int(input()) + final_order = list(map(lambda x: int(x) - 1, input().split())) + + solved = AdrenalineRushSolver(n, final_order).solve() + print(len(solved)) + for b, a in solved: + print(a + 1, b + 1) diff --git a/storage/zeta/rs/31159.rs b/storage/zeta/rs/31159.rs deleted file mode 100644 index 18351fb..0000000 --- a/storage/zeta/rs/31159.rs +++ /dev/null @@ -1,74 +0,0 @@ -use std::cmp::{max, min}; -use std::io::stdin; - -fn pair_matching(vs: &Vec<(i32, i32)>, i: usize, j: usize) -> i32 { - let (v0, v1) = (vs[i], vs[j]); - max( - max((v0.0 - v1.0).abs(), (v0.1 - v1.1).abs()), - max((v0.0 - v1.1).abs(), (v0.1 - v1.0).abs()), - ) -} - -fn solve_max_pair_matching(n: i32, vs: &Vec<(i32, i32)>) -> i32 { - let vs_index: Vec = (0..(2 * n as usize)).collect(); - - let mut vs_index_min = vs_index.clone(); - let mut vs_index_max = vs_index.clone(); - vs_index_min.sort_by_key(|&x| min(vs[x].0, vs[x].1)); - vs_index_max.sort_by_key(|&x| max(vs[x].0, vs[x].1)); - - let mut vs_index_presence = vec![true; 2 * n as usize]; - - let mut s = 0; - - let mut min_point = 0; - let mut max_point = 2 * n as usize - 1; - let mut cnt = 0; - while cnt < n { - if !vs_index_presence[vs_index_min[min_point]] { - min_point += 1; - continue; - } - if !vs_index_presence[vs_index_max[max_point]] { - max_point -= 1; - continue; - } - if vs_index_min[min_point] == vs_index_max[max_point] { - if min_point == 2 * n as usize - 1 { - max_point -= 1; - } else { - min_point += 1; - } - continue; - } - cnt += 1; - s += pair_matching(vs, vs_index_min[min_point], vs_index_max[max_point]); - vs_index_presence[vs_index_min[min_point]] = false; - vs_index_presence[vs_index_max[max_point]] = false; - min_point += 1; - max_point -= 1; - } - - s -} - -fn main() { - let mut line = String::new(); - stdin().read_line(&mut line).unwrap(); - let n = line.trim().parse::().unwrap(); - drop(line); - let vs = (0..(2 * n)) - .map(|_| { - let mut line = String::new(); - stdin().read_line(&mut line).unwrap(); - let v: Vec = line - .trim() - .split(' ') - .map(|x| x.parse::().unwrap()) - .collect(); - (v[0], v[1]) - }) - .collect::>(); - - println!("{}", solve_max_pair_matching(n, &vs)); -} diff --git a/storage/zeta/rs/completed/31159.rs b/storage/zeta/rs/completed/31159.rs new file mode 100644 index 0000000..f0cb61b --- /dev/null +++ b/storage/zeta/rs/completed/31159.rs @@ -0,0 +1,41 @@ +use std::io::stdin; + +fn solve_max_pair_matching(n: usize, vs: &Vec<(i64, i64)>) -> i64 { + let mut linear_scale: Vec = vs.iter().map(|&x| x.0 + x.1).collect(); + + let height_scale: Vec = vs + .iter() + .zip(linear_scale.iter()) + .map(|(&(a, _), &l)| (2 * a - l).abs()) + .collect(); + + linear_scale.sort(); + + let sum_height = height_scale.iter().sum::(); + let max_diff_linear = (0..n) + .map(|i| linear_scale[2 * n - i - 1] - linear_scale[i]) + .sum::(); + + (sum_height + max_diff_linear) / 2 +} + +fn main() { + let mut line = String::new(); + stdin().read_line(&mut line).unwrap(); + let n = line.trim().parse::().unwrap(); + drop(line); + let vs = (0..(2 * n)) + .map(|_| { + let mut line = String::new(); + stdin().read_line(&mut line).unwrap(); + let v: Vec = line + .trim() + .split(' ') + .map(|x| x.parse::().unwrap()) + .collect(); + (v[0], v[1]) + }) + .collect::>(); + + println!("{}", solve_max_pair_matching(n, &vs)); +}