CSES - Putka Open 2020 – 5/5 - Results
Submission details
Task:Järjestys
Sender:Hennkka
Submission time:2020-11-28 10:13:25 +0200
Language:Rust
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED26
#2ACCEPTED74
Test results
testverdicttimegroup
#1ACCEPTED0.01 s1, 2details
#2ACCEPTED0.01 s2details
#3ACCEPTED0.01 s2details

Code

use std::io::BufRead;

const MOD: usize = 1_000_000_007;

fn solve(a: Vec<usize>, x: usize) -> usize {
    let a = {
        let mut a = a;
        a.sort();
        a
    };
    let mut res = 1;
    for (i, v) in a.iter().enumerate() {
        // How many values are at most x smaller
        let k = (0..i)
            .rev()
            .map(|j| a[j])
            .take_while(|c| v - c <= x)
            .count();
        res *= k + 1;
        res %= MOD;
    }
    res
}

fn main() {
    let stdin = std::io::stdin();
    let stdin = stdin.lock();
    let mut lines = stdin.lines();

    let t: usize = lines.next().unwrap().unwrap().parse().unwrap();
    for _ in 0..t {
        let nx: Vec<usize> = lines
            .next()
            .unwrap()
            .unwrap()
            .split_whitespace()
            .map(|v| v.parse().unwrap())
            .collect();
        let (_n, x) = (nx[0], nx[1]);
        let input: Vec<usize> = lines
            .next()
            .unwrap()
            .unwrap()
            .split_whitespace()
            .map(|v| v.parse().unwrap())
            .collect();
        println!("{}", solve(input, x));
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_sample() {
        assert_eq!(solve(vec![1], 5), 1);
        assert_eq!(solve(vec![1, 2, 3], 1), 4);
        assert_eq!(solve(vec![2, 100, 95, 999, 1, 77, 6, 8], 42), 144);
    }
}

Test details

Test 1

Group: 1, 2

Verdict: ACCEPTED

input
100
5 20
65 49 97 55 9
6 33
42 32 10 92 60 37
...

correct output
6
192
4
72
288
...

user output
6
192
4
72
288
...
Truncated

Test 2

Group: 2

Verdict: ACCEPTED

input
100
85 980
570 804 393 119 973 743 345 46...

correct output
753889928
541098438
241592657
827522722
403761060
...

user output
753889928
541098438
241592657
827522722
403761060
...
Truncated

Test 3

Group: 2

Verdict: ACCEPTED

input
100
100 1
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
988185646
196898158
546822979
56844088
346866189
...

user output
988185646
196898158
546822979
56844088
346866189
...
Truncated