結果
| 問題 | No.4 おもりと天秤 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2020-12-03 13:57:35 | 
| 言語 | Rust (1.83.0 + proconio) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 2 ms / 5,000 ms | 
| コード長 | 2,631 bytes | 
| コンパイル時間 | 17,422 ms | 
| コンパイル使用メモリ | 378,028 KB | 
| 実行使用メモリ | 6,948 KB | 
| 最終ジャッジ日時 | 2024-09-13 23:26:23 | 
| 合計ジャッジ時間 | 14,059 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 23 | 
ソースコード
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]
            })
        );
    }
}
            
            
            
        