CSES - Datatähti 2021 loppu - Results
Submission details
Task:Järjestäminen
Sender:antti_p
Submission time:2021-01-23 18:03:40 +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);
    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 {
                    println!("{}", [first].iter().cycle().take(size).collect::<String>());
                    return
                }
            }
            '\n' => {
                break;
            }
            _ => panic!(),
        }
    }
    if first != '9' {
        println!("{}", [((first as u8 + 1) as char)].iter().cycle().take(size).collect::<String>());
    } else {
        println!("{}", ['1'].iter().cycle().take(size + 1).collect::<String>());
    }
}
*/

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

            if pos - 2 == n {
                continue
            }
            pos = pos - 2;
        }
        let tmp = a[pos];
        a[pos] = a[n];
        a[n] = tmp;

        let tmp = a[pos + 1];
        a[pos + 1] = a[n + 1];
        a[n + 1] = 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() - 1).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
NO
...

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

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

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