双向广搜 70pts 求助

P4799 [CEOI2015 Day2] 世界冰球锦标赛

Ysgg11 @ 2024-02-17 17:05:49

n, w = map(int, input().split())
lst = list(map(int, input().split()))
mid = n // 2

def dfs(original, l, r, path, operate):
    if l == r:
        operate.append(path)
        return operate
    dfs(original, l + 1, r, path, operate)
    path += original[l]
    dfs(original, l + 1, r, path, operate)
    return operate

left = dfs(lst, 0, n >> 1, 0, [])
left.sort()
right = dfs(lst, n >> 1, n, 0, [])
right.sort()

l, r = 0, (2 ** (n - mid)) - 1

ans = 0
while l < (2 ** mid) and r >= 0:
    if left[l] + right[r] <= w:
        ans += r + 1
        l += 1
    else:
        r -= 1

print(ans)

by Hugo_Von @ 2024-08-22 13:18:59

《双向广搜》
这不是dfs吗?


|