結果

問題 No.2042 RGB Caps
ユーザー phsplsphspls
提出日時 2022-09-19 22:59:50
言語 Rust
(1.77.0)
結果
AC  
実行時間 53 ms / 2,000 ms
コード長 2,082 bytes
コンパイル時間 1,065 ms
コンパイル使用メモリ 151,136 KB
実行使用メモリ 13,788 KB
最終ジャッジ日時 2023-08-23 19:10:31
合計ジャッジ時間 2,626 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 1 ms
4,380 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 17 ms
5,416 KB
testcase_08 AC 32 ms
9,268 KB
testcase_09 AC 6 ms
4,376 KB
testcase_10 AC 48 ms
12,488 KB
testcase_11 AC 4 ms
4,380 KB
testcase_12 AC 52 ms
13,744 KB
testcase_13 AC 52 ms
13,788 KB
testcase_14 AC 53 ms
13,788 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 14 ms
5,132 KB
testcase_17 AC 5 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn main() {
    let mut nk = String::new();
    std::io::stdin().read_line(&mut nk).ok();
    let nk: Vec<usize> = nk.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let n = nk[0];
    let k = nk[1];
    let mut patterns = (0..k).map(|_| {
            let mut temp = String::new();
            std::io::stdin().read_line(&mut temp).ok();
            let temp: Vec<&str> = temp.trim().split_whitespace().collect();
            (temp[0].parse::<usize>().unwrap(), temp[1].chars().nth(0).unwrap())
        })
        .collect::<Vec<(usize, char)>>();

    patterns.sort_by_key(|&(a, _)| a);
    let mut result = vec![];
    let mut cnt = vec![0usize; 3];
    let rgb = "RGB".chars().collect::<Vec<char>>();
    for i in 0..k {
        let (a, c) = patterns[i];
        let cval = if c == 'R' { 0 } else if c == 'G' { 1 } else { 2 };
        let mut addval = vec![0usize; 3];
        let mut diff = a - cnt.iter().sum::<usize>();
        let maxval = *cnt.iter().max().unwrap();
        let minval = *cnt.iter().min().unwrap();
        if minval < maxval {
            if cnt[cval] == minval {
                diff -= 1;
                addval[cval] += 1;
            }
            for i in 0..3 {
                if cnt[i] + addval[i] == minval && diff > 0 {
                    addval[i] += 1;
                    diff -= 1;
                }
            }
        }
        addval[0] += diff/3;
        addval[1] += diff/3;
        addval[2] += diff/3;
        diff %= 3;
        if diff == 2 {
            addval[cval] += 1;
            addval[(cval + 1) % 3] += 1;
        } else if diff == 1 {
            addval[cval] += 1;
        }
        for i in 0..3 {
            result.push(rgb[i].to_string().repeat(addval[i]));
            cnt[i] += addval[i];
        }
        let maxval = *cnt.iter().max().unwrap();
        if maxval != cnt[cval] {
            println!("-1");
            return;
        }
    }
    if cnt.iter().sum::<usize>() < n {
        result.push("R".repeat(n - cnt.iter().sum::<usize>()));
    }
    println!("{}", result.join(""));
}
0