From 0a52264f6ce697cbaf399f69742445807d252f33 Mon Sep 17 00:00:00 2001 From: yenru0 Date: Mon, 13 Jul 2026 14:27:21 +0900 Subject: [PATCH] complete 8566.rs --- storage/jungol/rs/completed/8566.rs | 38 +++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 storage/jungol/rs/completed/8566.rs diff --git a/storage/jungol/rs/completed/8566.rs b/storage/jungol/rs/completed/8566.rs new file mode 100644 index 0000000..f392900 --- /dev/null +++ b/storage/jungol/rs/completed/8566.rs @@ -0,0 +1,38 @@ +use std::{ + cmp::Reverse, + collections::BinaryHeap, + io::{read_to_string, stdin}, +}; + +fn main() { + let temp = read_to_string(stdin()).unwrap(); + let mut iter = temp + .split_ascii_whitespace() + .map(|x| x.parse::().unwrap()); + + let (n, p) = (iter.next().unwrap(), iter.next().unwrap()); + + let defs = (0..n).map(|_| iter.next().unwrap()).collect::>(); + + let mut heap = BinaryHeap::new(); + let mut total = 0; + let mut res = Vec::new(); + + for &d in defs.iter() { + heap.push(Reverse(d)); + total += d; + + while heap.len() > 0 && total - heap.peek().unwrap().0 >= p { + total -= heap.peek().unwrap().0; + heap.pop(); + } + if total >= p { + res.push(heap.len() as isize); + } else { + res.push(-1); + } + } + + res.iter().for_each(|x| print!("{} ", x)); + println!(); +}