結果

問題 No.2453 Seat Allocation
ユーザー haihamabossuhaihamabossu
提出日時 2023-09-01 23:23:26
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 61 ms / 2,000 ms
コード長 2,666 bytes
コンパイル時間 12,354 ms
コンパイル使用メモリ 403,400 KB
実行使用メモリ 13,412 KB
最終ジャッジ日時 2024-06-25 09:33:39
合計ジャッジ時間 14,609 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

pub mod scanner {

    pub struct Scanner {
        buf: Vec<String>,
    }

    impl Scanner {
        pub fn new() -> Self {
            Self { buf: vec![] }
        }

        pub fn new_from(source: &str) -> Self {
            let source = String::from(source);
            let buf = Self::split(source);
            Self { buf }
        }

        pub fn next<T: std::str::FromStr>(&mut self) -> T {
            loop {
                if let Some(x) = self.buf.pop() {
                    return x.parse().ok().expect("");
                }
                let mut source = String::new();
                std::io::stdin().read_line(&mut source).expect("");
                self.buf = Self::split(source);
            }
        }

        fn split(source: String) -> Vec<String> {
            source
                .split_whitespace()
                .rev()
                .map(String::from)
                .collect::<Vec<_>>()
        }
    }
}

use crate::scanner::Scanner;
use std::{collections::BinaryHeap, io::Write};

fn main() {
    let mut scanner = Scanner::new();
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    let t: usize = 1;
    for _ in 0..t {
        solve(&mut scanner, &mut out);
    }
}

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

impl PartialOrd for P {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        let x = self.a * other.b;
        let y = other.a * self.b;
        if x == y {
            other.i.partial_cmp(&self.i)
        } else {
            x.partial_cmp(&y)
        }
    }
}

impl Ord for P {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        let x = self.a * other.b;
        let y = other.a * self.b;
        if x == y {
            other.i.cmp(&self.i)
        } else {
            x.cmp(&y)
        }
    }
}

fn solve(scanner: &mut Scanner, out: &mut std::io::BufWriter<std::io::StdoutLock>) {
    let n: usize = scanner.next();
    let m: usize = scanner.next();
    let a = (0..n).map(|_| scanner.next::<usize>()).collect::<Vec<_>>();
    let b = (0..m).map(|_| scanner.next::<usize>()).collect::<Vec<_>>();
    let mut que = BinaryHeap::new();
    for i in 0..n {
        que.push(P {
            a: a[i],
            b: b[0],
            i,
            j: 0,
        });
    }
    for _ in 0..m {
        let p = que.pop().unwrap();
        writeln!(out, "{}", p.i + 1).unwrap();
        if p.j < m - 1 {
            que.push(P {
                a: a[p.i],
                b: b[p.j + 1],
                i: p.i,
                j: p.j + 1,
            });
        }
    }
}
0