結果

問題 No.2453 Seat Allocation
コンテスト
ユーザー northward
提出日時 2023-09-02 09:28:51
言語 Rust
(1.94.0 + proconio + num + itertools)
コンパイル:
/usr/bin/rustc_custom
実行:
./target/release/main
結果
AC  
実行時間 35 ms / 2,000 ms
+ 650µs
コード長 2,741 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 625 ms
コンパイル使用メモリ 201,656 KB
実行使用メモリ 9,396 KB
最終ジャッジ日時 2026-07-12 09:29:52
合計ジャッジ時間 2,650 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: `Eq::assert_receiver_is_total_eq` should never be implemented by hand
  --> src/main.rs:66:5
   |
66 |     fn assert_receiver_is_total_eq(&self) {}
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: this method was used to add checks to the `Eq` derive macro
   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
   = note: for more information, see issue #152336 <https://github.com/rust-lang/rust/issues/152336>
   = note: `#[warn(internal_eq_trait_method_impls)]` (part of `#[warn(future_incompatible)]`) on by default

ソースコード

diff #
raw source code

#![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