Solution for Reversort

main
Inga 🏳‍🌈 4 years ago
parent 44c55e4666
commit 7ae2e8150a
  1. 5
      .gitignore
  2. 9
      0327-Qualification/task1-reversort/Cargo.toml
  3. 35
      0327-Qualification/task1-reversort/src/main.rs

5
.gitignore vendored

@ -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

@ -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]

@ -0,0 +1,35 @@
use std::{io::{self, BufRead}};
trait PositionMinExt {
fn position_min(&self) -> usize;
}
impl<T: Ord> PositionMinExt for [T] {
fn position_min(&self) -> usize {
self.iter().enumerate().min_by_key(|&(_position, value)| value).unwrap().0
}
}
fn solve(mut list: Vec<u32>) -> 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<u32> = lines.next().unwrap().unwrap().split(char::is_whitespace).map(|part| part.parse().unwrap()).collect();
println!("Case #{}: {}", t, solve(list));
}
}
Loading…
Cancel
Save