complete 8566.rs
This commit is contained in:
38
storage/jungol/rs/completed/8566.rs
Normal file
38
storage/jungol/rs/completed/8566.rs
Normal file
@@ -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::<usize>().unwrap());
|
||||
|
||||
let (n, p) = (iter.next().unwrap(), iter.next().unwrap());
|
||||
|
||||
let defs = (0..n).map(|_| iter.next().unwrap()).collect::<Vec<_>>();
|
||||
|
||||
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!();
|
||||
}
|
||||
Reference in New Issue
Block a user