結果

問題 No.2453 Seat Allocation
ユーザー nautnaut
提出日時 2023-09-02 09:28:51
言語 Rust
(1.77.0)
結果
AC  
実行時間 49 ms / 2,000 ms
コード長 2,741 bytes
コンパイル時間 696 ms
コンパイル使用メモリ 154,556 KB
実行使用メモリ 7,892 KB
最終ジャッジ日時 2023-09-07 15:38:22
合計ジャッジ時間 2,605 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 30 ms
7,892 KB
testcase_06 AC 13 ms
4,380 KB
testcase_07 AC 7 ms
6,316 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 49 ms
7,792 KB
testcase_10 AC 47 ms
7,728 KB
testcase_11 AC 47 ms
7,764 KB
testcase_12 AC 12 ms
4,376 KB
testcase_13 AC 16 ms
4,380 KB
testcase_14 AC 12 ms
4,376 KB
testcase_15 AC 19 ms
4,376 KB
testcase_16 AC 15 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 30 ms
5,196 KB
testcase_19 AC 36 ms
6,560 KB
testcase_20 AC 14 ms
4,380 KB
testcase_21 AC 19 ms
4,380 KB
testcase_22 AC 27 ms
4,384 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(non_snake_case, unused_imports, unused_must_use)]
use std::cmp::Reverse;
use std::io::{self, prelude::*};
use std::str;

fn main() {
    let (stdin, stdout) = (io::stdin(), io::stdout());
    let mut scan = Scanner::new(stdin.lock());
    let mut out = io::BufWriter::new(stdout.lock());

    macro_rules! input {
        ($T: ty) => {
            scan.token::<$T>()
        };
        ($T: ty, $N: expr) => {
            (0..$N).map(|_| scan.token::<$T>()).collect::<Vec<_>>()
        };
    }

    let N = input!(usize);
    let M = input!(usize);
    let A = input!(usize, N);
    let B = input!(usize, M);

    let mut hq = std::collections::BinaryHeap::new();

    for i in 0..N {
        hq.push((Ratio::new(A[i], B[0]), Reverse(i), 0));
    }

    for _ in 0..M {
        let (_, i_rev, j) = hq.pop().unwrap();
        let i = i_rev.0;

        if j + 1 < M {
            hq.push((Ratio::new(A[i], B[j + 1]), Reverse(i), j + 1));
        }

        writeln!(out, "{}", i + 1);
    }
}

struct Ratio {
    a: usize,
    b: usize,
}

impl Ratio {
    fn new(a: usize, b: usize) -> Self {
        return Self { a: a, b: b };
    }
}

impl PartialEq for Ratio {
    fn eq(&self, other: &Self) -> bool {
        let a1 = self.a;
        let b1 = self.b;
        let a2 = other.a;
        let b2 = other.b;

        a1 * b2 == a2 * b1
    }
}

impl Eq for Ratio {
    fn assert_receiver_is_total_eq(&self) {}
}

impl PartialOrd for Ratio {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        let a1 = self.a;
        let b1 = self.b;
        let a2 = other.a;
        let b2 = other.b;

        (a1 * b2).partial_cmp(&(a2 * b1))
    }
}

impl Ord for Ratio {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        let a1 = self.a;
        let b1 = self.b;
        let a2 = other.a;
        let b2 = other.b;

        (a1 * b2).cmp(&(a2 * b1))
    }
}

struct Scanner<R> {
    reader: R,
    buf_str: Vec<u8>,
    buf_iter: str::SplitWhitespace<'static>,
}
impl<R: BufRead> Scanner<R> {
    fn new(reader: R) -> Self {
        Self {
            reader,
            buf_str: vec![],
            buf_iter: "".split_whitespace(),
        }
    }
    fn token<T: str::FromStr>(&mut self) -> T {
        loop {
            if let Some(token) = self.buf_iter.next() {
                return token.parse().ok().expect("Failed parse");
            }
            self.buf_str.clear();
            self.reader
                .read_until(b'\n', &mut self.buf_str)
                .expect("Failed read");
            self.buf_iter = unsafe {
                let slice = str::from_utf8_unchecked(&self.buf_str);
                std::mem::transmute(slice.split_whitespace())
            }
        }
    }
}
0