結果

問題 No.2623 Room Allocation
ユーザー nautnaut
提出日時 2024-02-09 22:37:42
言語 Rust
(1.77.0)
結果
AC  
実行時間 57 ms / 2,000 ms
コード長 2,351 bytes
コンパイル時間 940 ms
コンパイル使用メモリ 186,828 KB
実行使用メモリ 15,800 KB
最終ジャッジ日時 2024-02-09 22:37:45
合計ジャッジ時間 2,788 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 AC 1 ms
6,676 KB
testcase_04 AC 1 ms
6,676 KB
testcase_05 AC 0 ms
6,676 KB
testcase_06 AC 15 ms
7,424 KB
testcase_07 AC 15 ms
7,424 KB
testcase_08 AC 16 ms
7,808 KB
testcase_09 AC 15 ms
7,424 KB
testcase_10 AC 10 ms
8,192 KB
testcase_11 AC 10 ms
8,192 KB
testcase_12 AC 6 ms
6,676 KB
testcase_13 AC 6 ms
6,676 KB
testcase_14 AC 54 ms
15,800 KB
testcase_15 AC 19 ms
6,676 KB
testcase_16 AC 5 ms
6,676 KB
testcase_17 AC 1 ms
6,676 KB
testcase_18 AC 18 ms
6,676 KB
testcase_19 AC 18 ms
6,676 KB
testcase_20 AC 17 ms
6,676 KB
testcase_21 AC 18 ms
6,676 KB
testcase_22 AC 14 ms
6,676 KB
testcase_23 AC 8 ms
6,676 KB
testcase_24 AC 19 ms
6,676 KB
testcase_25 AC 16 ms
6,676 KB
testcase_26 AC 2 ms
6,676 KB
testcase_27 AC 57 ms
13,184 KB
testcase_28 AC 24 ms
7,808 KB
testcase_29 AC 14 ms
6,676 KB
testcase_30 AC 54 ms
12,032 KB
testcase_31 AC 5 ms
6,676 KB
testcase_32 AC 11 ms
6,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(non_snake_case, unused_imports, unused_must_use)]
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 X = input!(usize);
    let Y = input!(usize);

    let mut P = vec![];
    let mut C = vec![];

    for _ in 0..N {
        let (p, c) = (input!(usize), input!(char));
        P.push(p);
        C.push(c);
    }

    let mut A = vec![0; X + Y];
    let mut B = vec![0; X + Y];

    for i in 0..N {
        let (p, c) = (P[i], C[i]);

        if c == 'A' {
            A[i % (X + Y)] += p;
        } else {
            B[i % (X + Y)] += p;
        }
    }

    let mut ans = 0;
    let mut A2 = vec![];
    let mut B2 = vec![];

    for i in 0..X + Y {
        let (a, b) = (A[i], B[i]);

        let m = std::cmp::min(a, b);
        ans += m;

        A2.push(a - m);
        B2.push(b - m);
    }

    A2.sort();
    A2.reverse();

    B2.sort();
    B2.reverse();

    for i in 0..X {
        if let Some(a) = A2.get(i) {
            ans += a;
        }
    }

    for i in 0..Y {
        if let Some(b) = B2.get(i) {
            ans += b;
        }
    }

    writeln!(out, "{}", ans);
}

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