結果

問題 No.2453 Seat Allocation
ユーザー StrorkisStrorkis
提出日時 2023-09-01 23:31:44
言語 Rust
(1.77.0)
結果
AC  
実行時間 180 ms / 2,000 ms
コード長 3,068 bytes
コンパイル時間 924 ms
コンパイル使用メモリ 151,360 KB
実行使用メモリ 6,776 KB
最終ジャッジ日時 2023-09-07 15:37:51
合計ジャッジ時間 4,686 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 162 ms
6,688 KB
testcase_06 AC 140 ms
4,376 KB
testcase_07 AC 24 ms
6,140 KB
testcase_08 AC 16 ms
4,376 KB
testcase_09 AC 179 ms
6,700 KB
testcase_10 AC 178 ms
6,692 KB
testcase_11 AC 180 ms
6,776 KB
testcase_12 AC 136 ms
4,376 KB
testcase_13 AC 144 ms
4,380 KB
testcase_14 AC 133 ms
4,376 KB
testcase_15 AC 150 ms
4,380 KB
testcase_16 AC 143 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 141 ms
4,384 KB
testcase_19 AC 149 ms
5,704 KB
testcase_20 AC 58 ms
4,380 KB
testcase_21 AC 100 ms
4,376 KB
testcase_22 AC 146 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

pub mod input {
    use std::io::{BufRead, ErrorKind};

    pub trait BytesRead {
        fn read_bytes(&mut self) -> Vec<u8>;
    }

    impl<R: BufRead> BytesRead for R {
        #[inline]
        fn read_bytes(&mut self) -> Vec<u8> {
            let mut res = Vec::new();
            loop {
                let buf = match self.fill_buf() {
                    Ok(buf) => buf,
                    Err(e) if e.kind() == ErrorKind::Interrupted => continue,
                    Err(e) => panic!("{}", e), 
                };
                let (done, used, buf) = match buf.iter().position(u8::is_ascii_whitespace) {
                    Some(i) => (i > 0 || res.len() > 0, i + 1, &buf[..i]),
                    None => (buf.is_empty(), buf.len(), buf),
                };
                res.extend_from_slice(buf);
                self.consume(used);
                if done {
                    return res;
                }
            }
        }
    }

    #[macro_export]
    macro_rules! read {
        ($r:expr, [$t:tt; $n:expr]) => ((0..$n).map(|_| read!($r, $t)).collect::<Vec<_>>());
        ($r:expr, [$t:tt]) => (read!($r, [$t; read!($r, usize)]));
        ($r:expr, ($($t:tt),*)) => (($(read!($r, $t)),*));
        ($r:expr, Bytes) => ($r.read_bytes());
        ($r:expr, String) => (String::from_utf8(read!($r, Bytes)).unwrap());
        ($r:expr, Usize1) => (read!($r, usize) - 1);
        ($r:expr, $t:ty) => (read!($r, String).parse::<$t>().unwrap());
    }

    #[macro_export]
    macro_rules! input {
        ($r:expr, $($($v:ident)* : $t:tt),* $(,)?) => {
            $(let $($v)* = read!($r, $t);)*
        };
    }
}

use std::cmp::Ordering;
use std::collections::BinaryHeap;

use input::BytesRead;

#[derive(Eq, PartialEq)]
struct Candidate {
    a: u64, b: u64,
    i: usize, j: usize,
}

impl Candidate {
    fn new(a: u64, b: u64, i: usize, j: usize) -> Self {
        Self {
            a, b,
            i, j,
        }
    }
}

impl Ord for Candidate {
    fn cmp(&self, other: &Self) -> Ordering {
        (self.a * other.b).cmp(&(other.a * self.b))
            .then_with(|| other.i.cmp(&self.i))
    }
}

impl PartialOrd for Candidate {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

fn run<R: std::io::BufRead, W: std::io::Write>(reader: &mut R, _writer: &mut W) {
    input! {
        reader,
        n: usize, m: usize,
        a: [u64; n],
        b: [u64; m],
    }

    let mut heap = BinaryHeap::new();
    for i in 0..n {
        heap.push(Candidate::new(a[i], b[0], i, 0));
    }

    for _ in 0..m {
        let candidate = heap.pop().unwrap();
        let i = candidate.i;
        println!("{}", i + 1);
        let j = candidate.j + 1;
        if j < m {
            heap.push(Candidate::new(a[i], b[j], i, j));
        }
    }
}

fn main() {
    let (stdin, stdout) = (std::io::stdin(), std::io::stdout());
    let mut reader = std::io::BufReader::new(stdin.lock());
    let mut writer = std::io::BufWriter::new(stdout.lock());
    run(&mut reader, &mut writer);
}
0