結果

問題 No.4 おもりと天秤
ユーザー TANIGUCHI KousukeTANIGUCHI Kousuke
提出日時 2015-06-01 18:10:07
言語 Ruby
(3.2.2)
結果
AC  
実行時間 449 ms / 5,000 ms
コード長 609 bytes
コンパイル時間 286 ms
コンパイル使用メモリ 11,292 KB
実行使用メモリ 19,540 KB
最終ジャッジ日時 2023-09-08 16:17:05
合計ジャッジ時間 5,031 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
15,096 KB
testcase_01 AC 78 ms
15,212 KB
testcase_02 AC 83 ms
15,100 KB
testcase_03 AC 81 ms
15,072 KB
testcase_04 AC 88 ms
15,216 KB
testcase_05 AC 282 ms
17,396 KB
testcase_06 AC 81 ms
15,128 KB
testcase_07 AC 284 ms
17,248 KB
testcase_08 AC 79 ms
15,092 KB
testcase_09 AC 449 ms
19,540 KB
testcase_10 AC 297 ms
17,820 KB
testcase_11 AC 83 ms
15,216 KB
testcase_12 AC 78 ms
15,264 KB
testcase_13 AC 77 ms
15,108 KB
testcase_14 AC 79 ms
15,264 KB
testcase_15 AC 78 ms
15,232 KB
testcase_16 AC 81 ms
15,224 KB
testcase_17 AC 78 ms
15,036 KB
testcase_18 AC 265 ms
17,256 KB
testcase_19 AC 258 ms
17,332 KB
testcase_20 AC 257 ms
17,028 KB
testcase_21 AC 248 ms
17,140 KB
testcase_22 AC 250 ms
17,216 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:18: warning: assigned but unused variable - n
Syntax OK

ソースコード

diff #

# Here your code !

def solve(w, t)
    dp = {}
    dp[0] = Array.new(w.size + 1, 0)
    (1..t).each do |j|
        dp[j] = Array.new(w.size + 1, 0)
        w.each_with_index do |k, i|
            candiate = [dp[j][i], dp[j][i] + k ]
            candiate.push(dp[j - k][i] + k) if j >= k 
            candiate.reject! {|v| v > j }              
            dp[j][i + 1] = candiate.max
        end
    end
    dp[t].include?(t)
end

n = gets.to_i
w = gets.split.map(&:to_i)

total = w.inject(0, &:+)

if total.odd?
    puts "impossible"
elsif solve(w,total/2)
    puts "possible"
else
    puts "impossible"
end
0