mirror of
https://gitlab.com/mfocko/Codeforces.git
synced 2024-12-22 11:51:24 +01:00
chore(rs): adjust the skeleton
* add data structures as a module with min-heap * switch ‹.next_vec› to ‹.collect› that also supports other collections than ‹Vec<T>› Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
d7af3b2543
commit
efed3b5976
1 changed files with 34 additions and 1 deletions
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
// region ‹use›
|
// region ‹use›
|
||||||
use self::aliases::*;
|
use self::aliases::*;
|
||||||
|
use self::data_structures::*;
|
||||||
use self::input::*;
|
use self::input::*;
|
||||||
use self::math::*;
|
use self::math::*;
|
||||||
use self::output::*;
|
use self::output::*;
|
||||||
|
@ -44,6 +45,38 @@ mod aliases {
|
||||||
pub type Q<T> = VecDeque<T>;
|
pub type Q<T> = VecDeque<T>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mod data_structures {
|
||||||
|
use std::cmp::{Ord, Reverse};
|
||||||
|
use std::collections::BinaryHeap;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct MinHeap<T> {
|
||||||
|
heap: BinaryHeap<Reverse<T>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Ord> MinHeap<T> {
|
||||||
|
pub fn new() -> MinHeap<T> {
|
||||||
|
MinHeap {
|
||||||
|
heap: BinaryHeap::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn push(&mut self, item: T) {
|
||||||
|
self.heap.push(Reverse(item))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pop(&mut self) -> Option<T> {
|
||||||
|
self.heap.pop().map(|Reverse(x)| x)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Ord> Default for MinHeap<T> {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
mod input {
|
mod input {
|
||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
|
@ -76,7 +109,7 @@ mod input {
|
||||||
front.parse::<T>().ok().unwrap()
|
front.parse::<T>().ok().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn next_vec<T: FromStr>(&mut self, n: usize) -> Vec<T> {
|
pub fn collect<C: FromIterator<T>, T: FromStr>(&mut self, n: usize) -> C {
|
||||||
(0..n).map(|_| self.next()).collect()
|
(0..n).map(|_| self.next()).collect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue