Submission #7592976


Source Code Expand

Copy
extern crate core;
use core::fmt;
use std::cmp::Ordering::{Equal, Greater, Less};
use std::io::*;
use std::str::FromStr;

#[allow(dead_code)]
fn read_option<T: FromStr>() -> Option<T> {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok()
}

#[allow(dead_code)]
fn read<T: FromStr>() -> T {
    let opt = read_option();
    opt.expect("failed to parse token")
}

// binary heap for PartialOrd
struct MyBinaryHeap<T> {
    data: Vec<T>,
}
impl<T: Clone> Clone for MyBinaryHeap<T> {
    fn clone(&self) -> Self {
        MyBinaryHeap {
            data: self.data.clone(),
        }
    }
}
impl<T: fmt::Debug> core::fmt::Debug for MyBinaryHeap<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_list().entries(self.data.iter()).finish()
    }
}
impl<T: PartialOrd> MyBinaryHeap<T> {
    pub fn new() -> MyBinaryHeap<T> {
        MyBinaryHeap { data: vec![] }
    }
    pub fn push(&mut self, item: T) {
        self.data.push(item);
        let mut now_index = self.data.len() - 1;
        while now_index > 0 {
            // 0 start
            let parent_index = (now_index - 1) / 2;
            let cmp_result = self.data[now_index].partial_cmp(&self.data[parent_index]);
            if cmp_result.is_none() {
                panic!("compare non comparable values");
            }
            if cmp_result.unwrap() == Less || cmp_result.unwrap() == Equal {
                break;
            }
            self.data.swap(now_index, parent_index);
            now_index = parent_index;
        }
    }
    pub fn pop(&mut self) -> Option<T> {
        if self.data.is_empty() {
            return None;
        }
        let ret = self.data.swap_remove(0);
        let mut now_index = 0;
        if self.data.is_empty() {
            return Some(ret);
        }
        loop {
            let child_index1 = (now_index + 1) * 2 - 1;
            let child_index2 = (now_index + 1) * 2;
            if child_index1 > self.data.len() - 1 {
                break;
            }
            let change_index = if child_index2 > self.data.len() - 1
                || self.data[child_index1] > self.data[child_index2]
            {
                child_index1
            } else {
                child_index2
            };
            let cmp_result = self.data[now_index].partial_cmp(&self.data[change_index]);
            if cmp_result.is_none() {
                panic!("compare non comparable");
            }
            if cmp_result.unwrap() == Greater || cmp_result.unwrap() == Equal {
                break;
            }
            self.data.swap(now_index, change_index);
            now_index = change_index;
        }
        Some(ret)
    }
}
// slow because when call next, popped every time.
impl<T: PartialOrd> Iterator for MyBinaryHeap<T> {
    type Item = T;

    fn next(&mut self) -> Option<T> {
        self.pop()
    }
}

#[test]
fn it_works() {
    let mut a = MyBinaryHeap::new();
    assert_eq!(a.pop(), None);
    a.push(1);
    a.push(5);
    a.push(0);
    a.push(100);
    assert_eq!(a.pop(), Some(100));
    assert_eq!(a.pop(), Some(5));
    assert_eq!(a.pop(), Some(1));
    assert_eq!(a.pop(), Some(0));
    assert_eq!(a.pop(), None);
    assert_eq!(a.pop(), None);
}

#[test]
fn iter_works() {
    let mut a = MyBinaryHeap::new();
    assert_eq!(a.pop(), None);
    a.push(1);
    a.push(5);
    a.push(0);
    a.push(100);
    let expected = vec![100, 5, 1, 0];
    for (i, v) in a.enumerate() {
        assert_eq!(expected[i], v);
    }
}
#[test]
#[should_panic]
fn nan_panic() {
    let mut a = MyBinaryHeap::new();
    a.push(1.5);
    a.push(std::f64::NAN);
}
fn main() {
    // let mut queue = MyBinaryHeap::new();
    let mut queue = std::collections::BinaryHeap::new();
    let n: i32 = read();
    let m: i64 = read();
    for _ in 0..n {
        let price: i64 = read();
        queue.push(price);
    }
    for _ in 0..m {
        let price = queue.pop().unwrap();
        // round
        queue.push(price / 2);
    }
    let mut sum = 0;
    for price in queue {
        sum += price;
    }
    println!("{}", sum);
}

Submission Info

Submission Time
Task D - Powerful Discount Tickets
User dekokun
Language Rust (1.15.1)
Score 400
Code Size 4493 Byte
Status AC
Exec Time 49 ms
Memory 4352 KB

Compile Error

warning: method is never used: `new`, #[warn(dead_code)] on by default
  --> ./Main.rs:43:5
   |
43 |       pub fn new() -> MyBinaryHeap<T> {
   |  _____^ starting here...
44 | |         MyBinaryHeap { data: vec![] }
45 | |     }
   | |_____^ ...ending here

warning: method is never used: `push`, #[warn(dead_code)] on by default
  --> ./Main.rs:46:5
   |
46 |     pub fn push(&mut self, item: T) {
   |     ^

Judge Result

Set Name All Sample
Score / Max Score 400 / 400 0 / 0
Status
AC × 23
AC × 4
Set Name Test Cases
All sample_01, sample_02, sample_03, sample_04, testcase_01, testcase_02, testcase_03, testcase_04, testcase_05, testcase_06, testcase_07, testcase_08, testcase_09, testcase_10, testcase_11, testcase_12, testcase_13, testcase_14, testcase_15, testcase_16, testcase_17, testcase_18, testcase_19
Sample sample_01, sample_02, sample_03, sample_04
Case Name Status Exec Time Memory
sample_01 AC 2 ms 4352 KB
sample_02 AC 2 ms 4352 KB
sample_03 AC 2 ms 4352 KB
sample_04 AC 2 ms 4352 KB
testcase_01 AC 26 ms 4352 KB
testcase_02 AC 10 ms 4352 KB
testcase_03 AC 40 ms 4352 KB
testcase_04 AC 49 ms 4352 KB
testcase_05 AC 18 ms 4352 KB
testcase_06 AC 39 ms 4352 KB
testcase_07 AC 10 ms 4352 KB
testcase_08 AC 39 ms 4352 KB
testcase_09 AC 26 ms 4352 KB
testcase_10 AC 11 ms 4352 KB
testcase_11 AC 24 ms 4352 KB
testcase_12 AC 37 ms 4352 KB
testcase_13 AC 8 ms 4352 KB
testcase_14 AC 47 ms 4352 KB
testcase_15 AC 49 ms 4352 KB
testcase_16 AC 12 ms 4352 KB
testcase_17 AC 2 ms 4352 KB
testcase_18 AC 2 ms 4352 KB
testcase_19 AC 41 ms 4352 KB