From 7ae2e8150ae5c35a3ae23c1ca5f0bb0182e708af Mon Sep 17 00:00:00 2001 From: inga-lovinde <52715130+inga-lovinde@users.noreply.github.com> Date: Sat, 27 Mar 2021 12:48:38 +0100 Subject: [PATCH] Solution for Reversort --- .gitignore | 5 ++- 0327-Qualification/task1-reversort/Cargo.toml | 9 +++++ .../task1-reversort/src/main.rs | 35 +++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 0327-Qualification/task1-reversort/Cargo.toml create mode 100644 0327-Qualification/task1-reversort/src/main.rs diff --git a/.gitignore b/.gitignore index 088ba6b..0c28e35 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ # Generated by Cargo # will have compiled files and executables -/target/ +*/*/target/ # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html @@ -8,3 +8,6 @@ Cargo.lock # These are backup files generated by rustfmt **/*.rs.bk + +*.in +*.out diff --git a/0327-Qualification/task1-reversort/Cargo.toml b/0327-Qualification/task1-reversort/Cargo.toml new file mode 100644 index 0000000..b067331 --- /dev/null +++ b/0327-Qualification/task1-reversort/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "task1-reversort" +version = "0.1.0" +authors = ["inga-lovinde <52715130+inga-lovinde@users.noreply.github.com>"] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/0327-Qualification/task1-reversort/src/main.rs b/0327-Qualification/task1-reversort/src/main.rs new file mode 100644 index 0000000..657737f --- /dev/null +++ b/0327-Qualification/task1-reversort/src/main.rs @@ -0,0 +1,35 @@ +use std::{io::{self, BufRead}}; + +trait PositionMinExt { + fn position_min(&self) -> usize; +} + +impl PositionMinExt for [T] { + fn position_min(&self) -> usize { + self.iter().enumerate().min_by_key(|&(_position, value)| value).unwrap().0 + } +} + +fn solve(mut list: Vec) -> usize { + let mut result = 0; + + for i in 0..list.len()-1 { + let j = i + list[i..].position_min(); + list[i..=j].reverse(); + result += list[i..=j].len(); + } + + result +} + +fn main() { + let stdin = io::stdin(); + let mut lines = stdin.lock().lines(); + + let test_number: u32 = lines.next().unwrap().unwrap().parse().unwrap(); + for t in 1..=test_number { + let _length: u32 = lines.next().unwrap().unwrap().parse().unwrap(); + let list: Vec = lines.next().unwrap().unwrap().split(char::is_whitespace).map(|part| part.parse().unwrap()).collect(); + println!("Case #{}: {}", t, solve(list)); + } +}