結果

問題 No.4 おもりと天秤
ユーザー 綾地寧々綾地寧々
提出日時 2016-08-09 16:59:59
言語 PHP
(8.3.4)
結果
WA  
実行時間 -
コード長 810 bytes
コンパイル時間 897 ms
コンパイル使用メモリ 18,500 KB
実行使用メモリ 23,464 KB
最終ジャッジ日時 2023-08-07 04:52:14
合計ジャッジ時間 8,171 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
18,832 KB
testcase_01 AC 15 ms
18,792 KB
testcase_02 AC 17 ms
18,824 KB
testcase_03 WA -
testcase_04 AC 15 ms
18,852 KB
testcase_05 AC 15 ms
18,896 KB
testcase_06 AC 15 ms
18,912 KB
testcase_07 AC 15 ms
18,856 KB
testcase_08 AC 16 ms
18,804 KB
testcase_09 AC 16 ms
18,780 KB
testcase_10 AC 15 ms
18,848 KB
testcase_11 AC 15 ms
18,864 KB
testcase_12 AC 15 ms
18,848 KB
testcase_13 AC 16 ms
18,840 KB
testcase_14 AC 15 ms
18,912 KB
testcase_15 AC 16 ms
18,924 KB
testcase_16 WA -
testcase_17 AC 16 ms
18,840 KB
testcase_18 TLE -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
No syntax errors detected in Main.php

ソースコード

diff #

<?php
$n = trim(fgets(STDIN));
$w_array = explode(" ", trim(fgets(STDIN)));
rsort($w_array);
$w_target = array_sum($w_array);
if ( $w_target % 2 ) {
	echo 'impossible'.PHP_EOL;
	exit(0);
}
$w_target /= 2;

if ( ($w_array[0] == $w_target) || put_weight($w_array[0],1,1) ) {
	echo 'possible'.PHP_EOL;
}
else {
	echo 'impossible'.PHP_EOL;
}

function put_weight ( $put, $w_index, $w_count ) {
	global $w_array, $w_target, $n;
	if ( ($w_count >= $n) || ($w_index >= $n) ) {
		return FALSE;
	}
	if ( ($put + $w_array[$w_index]) > $w_target ) {
		return FALSE;
	}
	if ( ($put + $w_array[$w_index]) == $w_target ) {
		return TRUE;
	}
	$put += $w_array[$w_index];
	$ret = FALSE;
	for ( $i=$w_index+1; $i<$n; $i++ ) {
		$ret = put_weight($put, $i, $w_count+1);
		if ( $ret === TRUE ) {
			break;
		}
	}
	return $ret;
}
0