結果
問題 | No.4 おもりと天秤 |
ユーザー | はむ吉🐹 |
提出日時 | 2015-12-13 16:12:24 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 298 ms / 5,000 ms |
コード長 | 1,539 bytes |
コンパイル時間 | 88 ms |
コンパイル使用メモリ | 12,544 KB |
実行使用メモリ | 14,592 KB |
最終ジャッジ日時 | 2024-06-26 09:21:36 |
合計ジャッジ時間 | 3,433 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 32 ms
10,752 KB |
testcase_01 | AC | 31 ms
10,752 KB |
testcase_02 | AC | 35 ms
10,752 KB |
testcase_03 | AC | 32 ms
10,880 KB |
testcase_04 | AC | 33 ms
10,880 KB |
testcase_05 | AC | 270 ms
12,928 KB |
testcase_06 | AC | 31 ms
10,880 KB |
testcase_07 | AC | 277 ms
12,800 KB |
testcase_08 | AC | 31 ms
10,624 KB |
testcase_09 | AC | 87 ms
14,592 KB |
testcase_10 | AC | 298 ms
13,184 KB |
testcase_11 | AC | 32 ms
10,624 KB |
testcase_12 | AC | 31 ms
11,008 KB |
testcase_13 | AC | 32 ms
10,624 KB |
testcase_14 | AC | 31 ms
10,624 KB |
testcase_15 | AC | 32 ms
10,752 KB |
testcase_16 | AC | 32 ms
10,880 KB |
testcase_17 | AC | 31 ms
10,496 KB |
testcase_18 | AC | 159 ms
12,800 KB |
testcase_19 | AC | 248 ms
12,800 KB |
testcase_20 | AC | 245 ms
12,544 KB |
testcase_21 | AC | 234 ms
12,672 KB |
testcase_22 | AC | 239 ms
12,416 KB |
ソースコード
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import array UNDETERMINED = -1 # based on http://dai1741.github.io/maximum-algo-2012/docs/dynamic-programming/ class Knapsack(object): def __init__(self, number, weights): # Number of the weights. self.number = number # Weights of the weights. self.weights = weights # Array for memoization. self.memo = [array.array("i", [UNDETERMINED] * (sum(weights) + 1)) for x in range(number + 1)] def rec_memo(self, i, j): m = self.memo[i][j] if m != UNDETERMINED: return m elif i >= self.number: # if no weight is left result = 0 elif j < self.weights[i]: # if the weight i cannot be on the pan result = self.rec_memo(i + 1, j) else: # the value of the weight: the weight of the weight result = max(self.rec_memo(i + 1, j), self.rec_memo(i + 1, j - self.weights[i]) + self.weights[i]) self.memo[i][j] = result return result def solve(self, weight_limit, start_number=0): return self.rec_memo(start_number, weight_limit) def main(): n = int(input()) ws = array.array("i", (int(w) for w in input().split())) (d, m) = divmod(sum(ws), 2) if m == 1: print("impossible") else: k = Knapsack(n, ws) if k.solve(d) == d: print("possible") else: print("impossible") if __name__ == "__main__": main()