CSES - Datatähti 2021 loppu - Results
Submission details
Task:Järjestäminen
Sender:antti_p
Submission time:2021-01-23 16:57:52 +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 suurempi(s: &str) {
    let mut chars = s.chars().enumerate().skip_while(|&(_, x)| x == '0');
    let (size, first) = chars.next().unwrap();
    let size = s.len() - size;
    let mut chars = chars.map(|(_, x)| x);
    let mut exact = true;
    for c in chars {
        match c {
            '0'..='9' => {
                if c as u8 > first as u8 {
                    println!("{}", [((first as u8 + 1) as char)].iter().cycle().take(size).collect::<String>());
                    return;
                } else if (c as u8) < first as u8 {
                    exact = false;
                }
            }
            '\n' => {
                break;
            }
            _ => panic!(),
        }
    }
    if exact && first != '9' {
        println!("{}", [((first as u8 + 1) as char)].iter().cycle().take(size).collect::<String>());
    } else if exact {
        println!("{}", ['1'].iter().cycle().take(size + 1).collect::<String>());
    } else {
        println!("{}", [first].iter().cycle().take(size).collect::<String>());
    }
}
*/

fn jarjesta(a: &mut [usize]) -> bool {
    for n in 1..=a.len() {
        //println!("State of the array: {:?}", a);
        let mut pos = a.iter().enumerate().find(|(_, &x)| x == n).unwrap().0;
        if pos + 1 == n {
            continue
        } else if pos + 2 == n {
            if pos + 3 >= a.len() {
                //println!("Returning false from a");
                return false;
            }
            let tmp = a[pos - 1];
            a[pos - 1] = a[pos + 1];
            a[pos + 1] = tmp;

            let tmp = a[pos];
            a[pos] = a[pos + 2];
            a[pos + 2] = tmp;

            pos = pos + 2;
        } else if pos + 1 == a.len() {
            if n + 3 >= a.len() {
                //println!("Returning false from b");
                return false;
            }

            let tmp = a[pos - 1];
            a[pos - 1] = a[pos - 3];
            a[pos - 3] = tmp;

            let tmp = a[pos];
            a[pos] = a[pos - 2];
            a[pos - 2] = tmp;

            pos = pos - 2;
        }
        let tmp = a[pos];
        a[pos] = a[n - 1];
        a[n - 1] = tmp;

        let tmp = a[pos + 1];
        a[pos + 1] = a[n];
        a[n] = tmp;
    }
    true
}

fn main() {
    let stdin = io::stdin();
    let mut lines = stdin.lock().lines();
    let count = lines.next().unwrap().unwrap().parse::<usize>().unwrap();
    let mut ignore = true;
    for line in lines.take(count*2) {
        /*
        suurempi(&line.unwrap());
        */
        if !ignore {
            let mut v : Vec<_> = line.unwrap().split_whitespace().map(|x| x.parse::<usize>().unwrap()).collect();
            if jarjesta(&mut v) {
                println!("YES");
            } else {
                println!("NO");
            }
        }
        ignore = !ignore;
    }
}

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
YES
...
Truncated

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
YES
NO
YES
NO
NO
...
Truncated

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
NO
YES
...
Truncated

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
NO
NO
NO
NO
YES
...
Truncated