結果

問題 No.4 おもりと天秤
ユーザー Yusuke WadaYusuke Wada
提出日時 2020-11-26 15:04:08
言語 Rust
(1.72.1)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,739 bytes
コンパイル時間 912 ms
コンパイル使用メモリ 152,092 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-10-01 03:22:04
合計ジャッジ時間 1,868 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 3 ms
4,376 KB
testcase_19 AC 3 ms
4,376 KB
testcase_20 AC 3 ms
4,380 KB
testcase_21 AC 3 ms
4,380 KB
testcase_22 AC 3 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn getline() -> String {
    let mut __ret = String::new();
    std::io::stdin().read_line(&mut __ret).ok();
    return __ret;
}

fn getline_as_u32() -> u32 {
    let l = getline();
    let nlv: Vec<_> = l.trim().split(' ').collect();
    nlv[0].parse::<u32>().unwrap()
}

fn getline_as_u32_vec() -> Vec<u32> {
    let l = getline();
    let nlv = l.trim().split(' ');
    nlv.map(|x| x.parse::<u32>().unwrap()).collect()
}

fn main() {
    // 一つ前の状態を使う活動計画問題として考える
    let n: usize = getline_as_u32() as usize;
    let wv: Vec<u32> = getline_as_u32_vec();

    // dp[i番目までのおもり][片方の重さ] := i番目のおもりを使って片方の重さjが存在するか
    let mut dp: Vec<Vec<bool>> = (0..=n)
        .map(|_| (0..=n * 100).map(|_| false).collect())
        .collect();
    dp[0][0] = true;

    // DPループ iがDPの状態
    for i in 0..n {
        for j in 0..n * 100 {
            // DP 表 dp[i番目までのおもり][片方の重さ] を true にできるか

            // ひとつまえの選択状態をキャッチして、乗せるパターンとのせないパターンを記録する
            if dp[i][j] == true {
                // 選んだ場合、一つ前の重さ(J)jに i のおもりを加える
                dp[i + 1][j + wv[i] as usize] = true;
                // のせない場合、その重さが継続される
                dp[i + 1][j] = true;
            }
        }
    }

    // 全部の重さ/2になるようなDPが存在すればOK
    let sum = wv.into_iter().sum::<u32>();
    if sum % 2 == 0 && dp[n][sum as usize / 2] {
        println!("possible");
    } else {
        println!("impossible");
    }
}
0