結果

問題 No.4 おもりと天秤
ユーザー はむ吉🐹はむ吉🐹
提出日時 2015-12-13 16:12:24
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 242 ms / 5,000 ms
コード長 1,539 bytes
コンパイル時間 110 ms
コンパイル使用メモリ 10,968 KB
実行使用メモリ 12,804 KB
最終ジャッジ日時 2023-09-08 16:29:45
合計ジャッジ時間 3,070 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,572 KB
testcase_01 AC 19 ms
8,672 KB
testcase_02 AC 20 ms
8,608 KB
testcase_03 AC 20 ms
8,788 KB
testcase_04 AC 21 ms
8,604 KB
testcase_05 AC 217 ms
10,800 KB
testcase_06 AC 19 ms
8,672 KB
testcase_07 AC 233 ms
10,864 KB
testcase_08 AC 19 ms
8,572 KB
testcase_09 AC 64 ms
12,804 KB
testcase_10 AC 242 ms
11,192 KB
testcase_11 AC 19 ms
8,636 KB
testcase_12 AC 19 ms
8,636 KB
testcase_13 AC 19 ms
8,744 KB
testcase_14 AC 19 ms
8,732 KB
testcase_15 AC 19 ms
8,728 KB
testcase_16 AC 19 ms
8,772 KB
testcase_17 AC 19 ms
8,700 KB
testcase_18 AC 126 ms
10,568 KB
testcase_19 AC 202 ms
10,732 KB
testcase_20 AC 202 ms
10,604 KB
testcase_21 AC 192 ms
10,560 KB
testcase_22 AC 197 ms
10,652 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/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()
0