結果

問題 No.4 おもりと天秤
ユーザー YoshihitoYoshihito
提出日時 2020-12-03 13:57:35
言語 Rust
(1.72.1)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,631 bytes
コンパイル時間 6,102 ms
コンパイル使用メモリ 151,948 KB
実行使用メモリ 4,356 KB
最終ジャッジ日時 2023-10-11 23:28:31
合計ジャッジ時間 2,854 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

use std::io::{self, Read};

#[derive(Debug)]
struct Input {
    ws: Vec<usize>,
}

fn next_token(cin_lock: &mut io::StdinLock) -> String {
    cin_lock
        .by_ref()
        .bytes()
        .map(|c| c.unwrap() as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect::<String>()
}

fn read_input(cin_lock: &mut io::StdinLock) -> Input {
    let n = next_token(cin_lock).parse().unwrap();
    let ws = (0..n)
        .map(|_| next_token(cin_lock).parse().unwrap())
        .collect();
    Input { ws }
}

#[derive(Debug, Eq, PartialEq)]
struct Solution {
    possible: bool,
}

impl Solution {
    fn new(possible: bool) -> Solution {
        Solution { possible }
    }
}

fn simulate(input: Input) -> Solution {
    let sum = input.ws.iter().sum::<usize>();
    if sum % 2 != 0 {
        return Solution::new(false);
    }
    let half = sum / 2;

    let mut ws = input.ws;
    ws.sort();

    let mut dp = vec![vec![false; half + 1]; ws.len() + 1];
    dp[0][0] = true;
    for item_index in 0..ws.len() {
        if dp[item_index][half] {
            return Solution { possible: true };
        }

        for sum in 0..=half {
            dp[item_index + 1][sum] = dp[item_index][sum];
            if sum >= ws[item_index] {
                dp[item_index + 1][sum] |= dp[item_index][sum - ws[item_index]];
            }
        }
    }

    let possible = dp[ws.len()][half];
    Solution { possible }
}

fn solve(input: Input, _cin_lock: &mut io::StdinLock) {
    let answer = simulate(input);
    println!(
        "{}",
        if answer.possible {
            "possible"
        } else {
            "impossible"
        }
    );
}

fn main() {
    let cin = io::stdin();
    let mut cin_lock = cin.lock();

    let input = read_input(&mut cin_lock);
    solve(input, &mut cin_lock);
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn sample_verify() {
        assert_eq!(
            Solution::new(true),
            simulate(Input {
                ws: vec![62, 1, 90, 3, 24]
            })
        );
    }

    #[test]
    fn sample1() {
        assert_eq!(Solution::new(true), simulate(Input { ws: vec![1, 2, 3] }));
    }

    #[test]
    fn sample2() {
        assert_eq!(
            Solution::new(false),
            simulate(Input {
                ws: vec![1, 2, 3, 4, 5]
            })
        );
    }

    #[test]
    fn sample3() {
        assert_eq!(
            Solution::new(false),
            simulate(Input {
                ws: vec![62, 8, 90, 2, 24, 62, 38, 64, 76, 60, 30, 76, 80, 74, 72]
            })
        );
    }
}
0