結果

問題 No.4 おもりと天秤
ユーザー xoke0114xoke0114
提出日時 2018-03-13 20:07:29
言語 Rust
(1.72.1)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 2,758 bytes
コンパイル時間 2,589 ms
コンパイル使用メモリ 148,416 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-08 17:44:18
合計ジャッジ時間 3,659 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 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 4 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 4 ms
4,380 KB
testcase_08 AC 0 ms
4,376 KB
testcase_09 AC 4 ms
4,380 KB
testcase_10 AC 4 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,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 4 ms
4,376 KB
testcase_19 AC 4 ms
4,376 KB
testcase_20 AC 4 ms
4,376 KB
testcase_21 AC 4 ms
4,376 KB
testcase_22 AC 4 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(unused_imports, unused_variables, dead_code, non_snake_case, unused_macros)]
use std::io::{stdin, Read, StdinLock};
use std::str::FromStr;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

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

macro_rules! readl {
    ($t: ty) => {
        {
            let s = getline();
            s.trim().parse::<$t>().unwrap()
        }
    };
    ($( $t: ty),+ ) => {
        {
            let s = getline();
            let mut iter = s.trim().split(' ');
            ($(iter.next().unwrap().parse::<$t>().unwrap(),)*) 
        }
    };
}

macro_rules! readlvec {
    ($t: ty) => {
        {
            let s = getline();
            let iter = s.trim().split(' ');
            iter.map(|x| x.parse().unwrap()).collect::<Vec<$t>>()
        }
    }
}

macro_rules! mvec {
    ($v: expr, $s: expr) => {
        vec![$v; $s]
    };
    ($v: expr, $s: expr, $($t: expr),*) => {
        vec![mvec!($v, $($t),*); $s]
    };
}

macro_rules! debug {
    ($x: expr) => {
        println!("{}: {:?}", stringify!($x), $x)
    }
}

fn printiter<'a, T>(v: &'a T)
where
    &'a T: std::iter::IntoIterator, 
    <&'a T as std::iter::IntoIterator>::Item: std::fmt::Display {
    for (i,e) in v.into_iter().enumerate() {
        if i != 0 {
            print!(" ");
        }
        print!("{}", e);
    }
    println!("");
}

struct ContestPrinter {
    s: String,
}

impl ContestPrinter {
    fn new() -> ContestPrinter {
        ContestPrinter {
        s: String::new(),
        }
    }

    fn print<T>(&mut self, x: T)
        where T: std::fmt::Display {
        self.s.push_str(format!("{}", x).as_str());
    }

    fn println<T>(&mut self, x: T)
        where T: std::fmt::Display {
        self.s.push_str(format!("{}\n", x).as_str());
    }
}

impl std::ops::Drop for ContestPrinter {
    fn drop(&mut self) {
        print!("{}", self.s);
    }
}

fn is_max_i64(num: i64) -> bool { if num == i64::max_value() { true } else { false } }

fn main() {
    let mut pr = ContestPrinter::new();
    
    let N = readl!(usize);
    let ws = readlvec!(usize);
    let sum = ws.iter().fold(0, |sum, x| sum + x);
    if (sum % 2) != 0 {
        pr.println("impossible");
        return;
    }

    let possible_sum = 100 * 100 + 1;
    let mut dp = vec![vec![false; possible_sum]; N+1];
    dp[0][0] = true;
    for i in 1..N+1 {
        let w = ws[i-1];
        for j in 0..possible_sum {
            if j >= w {
                dp[i][j] = dp[i - 1][j] | dp[i - 1][j - w];
            }
        }
    }

    pr.println(
        if dp[N][sum / 2] {
            "possible"
        } else {
            "impossible"
        }
    );
}
0