Submission #7637429


Source Code Expand

Copy
use std::collections::BTreeSet;

fn main() {
    let s = std::io::stdin();
    let mut sc = Scanner { stdin: s.lock() };
    let n: usize = sc.read();
    let k: usize = sc.read();
    let p: Vec<u64> = sc.vec(n);
    let mut set = BTreeSet::new();
    let mut same_as_next = vec![];
    for i in 0..n {
        if set.len() < k {
            set.insert(p[i]);
            continue;
        }
        let max = *set.iter().next_back().unwrap();
        let min = *set.iter().next().unwrap();
        if max < p[i] && min == p[i - k] {
            same_as_next.push(i - k);
            //            eprintln!("{:?}", set);
        }
        set.insert(p[i]);
        set.remove(&p[i - k]);
    }

    let mut same_as_nothing = BTreeSet::new();
    let mut i = 0;
    while i < n {
        let mut to = i;
        for j in (i + 1)..n {
            if p[j - 1] < p[j] {
                to = j;
            } else {
                break;
            }
        }
        for from in i.. {
            if from + k - 1 > to {
                break;
            }
            same_as_nothing.insert(from);
        }
        i = to + 1;
    }

    //    eprintln!("{:?}", same_as_next);
    //    eprintln!("{:?}", same_as_nothing);
    let ignore1 = same_as_next
        .into_iter()
        .filter(|i| !same_as_nothing.contains(i))
        .count();
    let ignore2 = if same_as_nothing.is_empty() {
        0
    } else {
        same_as_nothing.len() - 1
    };
    println!("{}", n - k + 1 - ignore1 - ignore2);
}

pub struct Scanner<R> {
    stdin: R,
}

impl<R: std::io::Read> Scanner<R> {
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .stdin
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n')
            .take_while(|&b| b != b' ' && b != b'\n')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}

Submission Info

Submission Time
Task B - Sorting a Segment
User kenkoooo
Language Rust (1.15.1)
Score 700
Code Size 2377 Byte
Status AC
Exec Time 115 ms
Memory 12540 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 700 / 700
Status
AC × 3
AC × 32
Set Name Test Cases
Sample sample-01.txt, sample-02.txt, sample-03.txt
All 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 01-18.txt, 01-19.txt, 01-20.txt, 01-21.txt, 01-22.txt, 01-23.txt, 01-24.txt, 01-25.txt, 01-26.txt, 01-27.txt, 01-28.txt, 01-29.txt, sample-01.txt, sample-02.txt, sample-03.txt
Case Name Status Exec Time Memory
01-01.txt AC 2 ms 4352 KB
01-02.txt AC 2 ms 4352 KB
01-03.txt AC 76 ms 4352 KB
01-04.txt AC 68 ms 6396 KB
01-05.txt AC 43 ms 6396 KB
01-06.txt AC 76 ms 8444 KB
01-07.txt AC 100 ms 12540 KB
01-08.txt AC 69 ms 6396 KB
01-09.txt AC 115 ms 12540 KB
01-10.txt AC 64 ms 6396 KB
01-11.txt AC 77 ms 6396 KB
01-12.txt AC 78 ms 8444 KB
01-13.txt AC 73 ms 6396 KB
01-14.txt AC 90 ms 6396 KB
01-15.txt AC 80 ms 8444 KB
01-16.txt AC 83 ms 6396 KB
01-17.txt AC 92 ms 6396 KB
01-18.txt AC 81 ms 8444 KB
01-19.txt AC 83 ms 8444 KB
01-20.txt AC 107 ms 10492 KB
01-21.txt AC 93 ms 10492 KB
01-22.txt AC 87 ms 8444 KB
01-23.txt AC 79 ms 8444 KB
01-24.txt AC 81 ms 8444 KB
01-25.txt AC 78 ms 10492 KB
01-26.txt AC 78 ms 10492 KB
01-27.txt AC 81 ms 8444 KB
01-28.txt AC 77 ms 10492 KB
01-29.txt AC 77 ms 10492 KB
sample-01.txt AC 2 ms 4352 KB
sample-02.txt AC 2 ms 4352 KB
sample-03.txt AC 2 ms 4352 KB