practivceAlgorithm/백준 379

[백준][Python] 15789 CTP왕국은 한솔왕국을 이길 수 있을까?

bfs로 동맹 나누고 heap에 동맹 크기 큰 순서(최대힙)로 넣어서 K넘지안는선에서 동맹. import sys input = sys.stdin.readline from collections import deque from heapq import heappop,heappush def bfs(i): queue = deque() queue.append(i) check[i] = i cnt = 1 while queue: cur = queue.popleft() for next in graph[cur]: if not check[next]: queue.append(next) check[next] = i cnt += 1 return cnt N, M = map(int, input().split()) graph = {i:..

[백준][Python] 2422 한윤정이 이탈리아에 가서 아이스크림을 사먹는데

내 풀이. dfs 조합으로 무식하게 풀었다. import sys input = sys.stdin.readline def dfs(s,cnt): global ans if cnt==3: ans += 1 return if cnt > 3: return for next in range(s+1,N+1): if not eat[next] and next not in donot_list: eat[next] = True for hate in ice_cream[next]: donot_list.append(hate) dfs(next, cnt + 1) eat[next] = False for hate in ice_cream[next]: donot_list.pop() N, M = map(int, input().split()) ice..

[백준][Python] 15824 너 봄에는 캡사이신이 맛있단다.

핵심은 power함수의 logN화. 1번 50점. import sys input = sys.stdin.readline N = int(input()) menus = sorted(map(int,input().split())) MOD = 1000000007 minus = 0 for i in range(N-1): for j in range(i+1,N): minus += ((menus[j] - menus[i])*(2**(j-i-1)))%MOD print(minus%MOD) 2번 50점 N = int(input()) menus = sorted(map(int,input().split())) MOD = 1000000007 minus = 0 plus = 0 for i in range(N): minus += (menus[..

[백준][Python] 15823 카드팩 구매하기 : 이분탐색과 실패함수 비교

1. 투포인터 후 큰 숫자부터 cnt 50점 통과 import sys input = sys.stdin.readline from collections import defaultdict N, M = map(int, input().split()) cards = list(map(int, input().split())) cards.append(cards[-1]) # 연속된 카드 묶기. 정확히 M개의 카드팩. 같은 종류 카드 두장되면 안됨. # 카드팩의 최대 길이. left = 0 pack_cnt = [] card_pack = defaultdict(int) for right in range(N+1): card_pack[cards[right]] += 1 if card_pack[cards[right]] > 1: pac..