結果

問題 No.15 カタログショッピング
ユーザー square1001square1001
提出日時 2022-02-17 14:30:06
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 248 ms / 5,000 ms
コード長 857 bytes
コンパイル時間 226 ms
コンパイル使用メモリ 10,944 KB
実行使用メモリ 15,964 KB
最終ジャッジ日時 2023-09-11 17:40:23
合計ジャッジ時間 2,428 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,360 KB
testcase_01 AC 16 ms
8,264 KB
testcase_02 AC 17 ms
8,204 KB
testcase_03 AC 18 ms
8,348 KB
testcase_04 AC 16 ms
8,208 KB
testcase_05 AC 224 ms
15,776 KB
testcase_06 AC 235 ms
15,812 KB
testcase_07 AC 240 ms
15,896 KB
testcase_08 AC 248 ms
15,856 KB
testcase_09 AC 242 ms
15,964 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import bisect

# Input
N, S = map(int, input().split())
A = [ int(input()) for i in range(N) ]
A.reverse()

# Enumerate all
sep = N // 2
cost1 = [ 0 ] * (1 << sep)
for i in range(0, sep):
	for j in range(0, 1 << i):
		cost1[j | (1 << i)] = cost1[j] + A[i]
cost2 = [ 0 ] * (1 << (N - sep))
for i in range(0, N - sep):
	for j in range(0, 1 << i):
		cost2[j | (1 << i)] = cost2[j] + A[i + sep]

# Meet-in-the-middle
scost1 = sorted([ (cost1[i], i) for i in range(0, 1 << sep) ])
for i in range((1 << (N - sep)) - 1, -1, -1):
	l = bisect.bisect_left(scost1, (S - cost2[i], -1))
	r = bisect.bisect_left(scost1, (S - cost2[i] + 1, -1))
	for j in range(r - 1, l - 1, -1):
		vals = list(filter(lambda x: ((i >> (N - sep - x)) & 1) == 1, range(1, N - sep + 1))) + list(filter(lambda x: ((scost1[j][1] >> (N - x)) & 1) == 1, range(N - sep + 1, N + 1)))
		print(*vals)
0