CSES - Datatähti 2021 loppu - Results
Submission details
Task:Järjestäminen
Sender:xnor
Submission time:2021-01-23 16:51:19 +0200
Language:Rust
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
Test results
testverdicttimegroup
#10.01 s1, 2details
#20.01 s2details
#30.01 s1, 2details
#40.01 s1, 2details

Code

use std::io::{self, BufRead};

fn main() {
    let mut lines = io::BufReader::new(io::stdin()).lines();

    let t = lines.next().unwrap().unwrap().parse::<usize>().unwrap();

    let mut arrays: Vec<Vec<usize>> = Vec::new();

    for _ in 0..t {
        let _n = lines.next().unwrap().unwrap().parse::<usize>().unwrap();
        let array_line = lines.next().unwrap().unwrap();
        arrays.push(array_line.split_ascii_whitespace().map(|s| s.parse::<usize>().unwrap()).collect());
    }

    for array in &mut arrays {
        if try_sort(1, array) {
            println!("YES");
        } else {
            println!("NO");
        }
    }
}

fn try_sort(m: usize, array: &mut [usize]) -> bool {
    if array.len() == 1 {
        return true
    }
    if array.len() == 2 {
        return array[0] < array[1]
    }
    if array.len() == 3 {
        return array[0] < array[1] && array[1] < array[2]
    }

    if Some(&m) == array.iter().nth(1) {
        return false;
    }

    if m == array[0] {
        return try_sort(m + 1, &mut array[1..])
    } else {
        let i = array.iter().position(|x| *x == m).unwrap();

        let tmp1 = array[0];
        let tmp2 = array[1];

        array[0] = array[i];
        array[1] = array[i+1];
        
        array[i] = tmp1;
        array[i+1] = tmp2;

        return try_sort(m + 1, &mut array[1..])
    }
}

Test details

Test 1

Group: 1, 2

Verdict:

input
153
1
1
2
1 2
...

correct output
YES
YES
NO
NO
NO
...

user output
YES
YES
NO
NO
NO
...

Error:
thread 'main' panicked at 'index out of bounds: the len is 4 but the index is 4', input/code.rs:49:20
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Test 2

Group: 2

Verdict:

input
1000
59
35 29 32 50 11 15 9 21 19 45 2...

correct output
YES
NO
YES
NO
YES
...

user output
(empty)

Error:
thread 'main' panicked at 'index out of bounds: the len is 54 but the index is 54', input/code.rs:49:20
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Test 3

Group: 1, 2

Verdict:

input
720
6
1 6 4 5 2 3
6
6 3 2 1 5 4
...

correct output
YES
NO
NO
NO
YES
...

user output
NO
NO
NO

Error:
thread 'main' panicked at 'index out of bounds: the len is 6 but the index is 6', input/code.rs:49:20
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Test 4

Group: 1, 2

Verdict:

input
1000
8
7 4 2 8 6 3 5 1
8
3 8 2 7 5 4 6 1
...

correct output
NO
NO
YES
NO
YES
...

user output
(empty)

Error:
thread 'main' panicked at 'index out of bounds: the len is 8 but the index is 8', input/code.rs:49:20
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace