비트마스킹 3

[백준][Python] 1102 발전소

발전소 최솟값으로 키는 문제. 꺼져있으면 켜져있는 발전소중에 제일 싼 발전소로 선택해 키는게 상태값 최저가면 최신화. import sys input = sys.stdin.readline from collections import deque def make_electic(start,cur_cnt): queue = deque() dp[start] = 0 answer=float('inf') if target_cnt==0 or cur_cnt>=target_cnt: return 0 elif cur_cnt==0: return -1 else: queue.append([start,0]) while target_cnt > cur_cnt: # 한 순회에 하나씩 킬꺼임. circle_SIZE = len(queue) for ..

[백준][Python] 1208 부분수열의 합

부분수열또한 선택과 비선택의 비트마스킹이 잘어울리는 문제이다. 단지 index를 확실히 해둬야 가져가고 안가져가고를 선택하는데 도움이 될 것이다. first, second라는 dp배열을 양쪽에 두어 dp배열간 합이 s에 일치하는경우 각 합이 되는 경우의 수를 cnt해 곱하는 식으로 답을 찾아나간다. import sys # 입력부 n,s = map(int, sys.stdin.readline().split()) a = list(map(int, sys.stdin.readline().split())) # 이분 분할 m = n//2 n = n - m # first : 왼쪽 Subset # 왼쪽의 경우의 수. first = [0] * (1