結果

問題 No.4 おもりと天秤
ユーザー ゴリポン先生ゴリポン先生
提出日時 2023-09-12 16:53:21
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 6 ms / 5,000 ms
コード長 667 bytes
コンパイル時間 3,658 ms
コンパイル使用メモリ 161,516 KB
実行使用メモリ 6,000 KB
最終ジャッジ日時 2023-09-12 16:53:28
合計ジャッジ時間 4,907 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

module main;

import std;
// 鉄則本 初版P115より
void main()
{
	// 入力
	int N = readln.chomp.to!int;
	auto W = readln.split.to!(int[]);
	W = 0 ~ W;	// 1-originにする
	// 答えの計算
	int sum = W.sum;
	if (sum % 2 == 1) {
		// 重さの合計が奇数なら二等分できない
		writeln("impossible");
		return;
	}
	int half = sum / 2;
	auto dp = new bool[][](N + 1, sum + 1);
	dp[0][0] = true;
	foreach (i; 1 .. N + 1) {
		foreach (j; 0 .. sum + 1) {
			if (j < W[i]) {
				dp[i][j] = dp[i - 1][j];
			} else {
				dp[i][j] = dp[i - 1][j] || dp[i - 1][j - W[i]];
			}
		}
	}
	// 答えの出力
	writeln(dp[N][half] ? "possible" : "impossible");
}
0