Problem: 2061D - Kevin and Numbers
Language: Rust 2021

Previous submission (verdict: Wrong answer on test 3):
#![allow(unused)]



use std::{

    cmp::{max, min, Ordering, Reverse},

    collections::{BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque},

    io::stdin,

    mem::swap,

    ops::Index,

    str::FromStr,

    thread::current,

    usize,

};



fn take_usize() -> usize {

    let mut input = String::new();

    stdin().read_line(&mut input).unwrap();

    input.trim().parse().unwrap()

}



fn take_vector<T>() -> Vec<T>

where

    T: FromStr,

    <T as FromStr>::Err: std::fmt::Debug,

{

    let mut input = String::new();

    stdin().read_line(&mut input).unwrap();

    input

        .trim()

        .split_whitespace()

        .map(|x| T::from_str(x).unwrap())

        .collect()

}



fn take_vector_usize() -> Vec<usize> {

    take_vector()

}



fn take_vector_int() -> Vec<i64> {

    take_vector()

}



fn take_string() -> String {

    let mut input = String::new();

    stdin().read_line(&mut input).unwrap();

    input = input.trim().to_string();

    input

}



fn take_chars() -> Vec<char> {

    take_string().chars().collect()

}



fn create_vector<T>(n: usize, initial_value: T) -> Vec<T>

where

    T: Clone,

{

    (0..n).map(|_| initial_value.clone()).collect()

}



type VV<T> = Vec<Vec<T>>;



fn solve() {

    let input = take_vector_usize();

    let n = input[0];

    let m = input[1];



    let a = take_vector_usize();

    let mut b = take_vector_usize();



    let mut cnt: HashMap<usize, i64> = HashMap::with_capacity(n); 

    for el in a {

        cnt.insert(el, cnt.get(&el).unwrap_or(&0) + 1);

    }

    let mut pq = b.iter().map(|val| *val).collect::<BinaryHeap<_>>();



    while !pq.is_empty() && pq.len() <= n {

        let el = pq.pop().unwrap();



        if let Some(i) = cnt.get(&el) {

            cnt.insert(el, i - 1);

        } else {

            let v1 = f64::ceil(el as f64 / 2f64) as i64;

            let v2 = f64::floor(el as f64 / 2f64) as i64;



            if v1 == 0 || v2 == 0 {

                println!("NO");

                return;

            }



            pq.push(v1 as usize);

            pq.push(v2 as usize);

        }

    }



    if cnt.values().sum::<i64>() != 0 || pq.len() > 0 {

        println!("NO");

    } else {

        println!("YES");

    }



}



pub fn main() {

    let t = take_usize();



    for t in 0..t {

        solve();

    }



    // solve();

}



Submission (verdict: Accepted):
#![allow(unused)]



use std::{

    cmp::{max, min, Ordering, Reverse},

    collections::{BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque},

    io::stdin,

    mem::swap,

    ops::Index,

    str::FromStr,

    thread::current,

    usize,

};



fn take_usize() -> usize {

    let mut input = String::new();

    stdin().read_line(&mut input).unwrap();

    input.trim().parse().unwrap()

}



fn take_vector<T>() -> Vec<T>

where

    T: FromStr,

    <T as FromStr>::Err: std::fmt::Debug,

{

    let mut input = String::new();

    stdin().read_line(&mut input).unwrap();

    input

        .trim()

        .split_whitespace()

        .map(|x| T::from_str(x).unwrap())

        .collect()

}



fn take_vector_usize() -> Vec<usize> {

    take_vector()

}



fn take_vector_int() -> Vec<i64> {

    take_vector()

}



fn take_string() -> String {

    let mut input = String::new();

    stdin().read_line(&mut input).unwrap();

    input = input.trim().to_string();

    input

}



fn take_chars() -> Vec<char> {

    take_string().chars().collect()

}



fn create_vector<T>(n: usize, initial_value: T) -> Vec<T>

where

    T: Clone,

{

    (0..n).map(|_| initial_value.clone()).collect()

}



type VV<T> = Vec<Vec<T>>;



fn solve() {

    let input = take_vector_usize();

    let n = input[0];

    let m = input[1];



    let a = take_vector_usize();

    let mut b = take_vector_usize();



    let mut cnt: HashMap<usize, i64> = HashMap::with_capacity(n); 

    for el in a {

        cnt.insert(el, cnt.get(&el).unwrap_or(&0) + 1);

    }

    let mut pq = b.iter().map(|val| *val).collect::<BinaryHeap<_>>();



    while !pq.is_empty() && pq.len() <= n {

        let el = pq.pop().unwrap();



        let x = cnt.get(&el).unwrap_or(&0);

        if *x > 0 {

            cnt.insert(el, x - 1);

        } else {

            let v1 = f64::ceil(el as f64 / 2f64) as i64;

            let v2 = f64::floor(el as f64 / 2f64) as i64;



            if v1 == 0 || v2 == 0 {

                println!("NO");

                return;

            }



            pq.push(v1 as usize);

            pq.push(v2 as usize);

        }

    }



    if cnt.values().sum::<i64>() != 0 || pq.len() > 0 {

        println!("NO");

    } else {

        println!("YES");

    }



}



pub fn main() {

    let t = take_usize();



    for t in 0..t {

        solve();

    }



    // solve();

}