practivceAlgorithm 570

[백준][Python] 6568 귀도 반 로썸은 크리스마스날 심심하다고 파이썬을 만들었다.

비트문제라기엔 EOF출력이 더 어려웠다.. import sys input = sys.stdin.readline while True: memory = [0 for _ in range(32)] cal = 0 pc = 0 for i in range(32): try: memory[i] = int(input().rstrip(),2) except EOFError: exit() while True: adress = memory[pc] cmd = adress//32 value = adress%32 pc = (pc + 1)%32 if cmd == 0: memory[value] = cal elif cmd == 1: cal = memory[value] elif cmd == 2: if not cal: pc = value el..

[백준][Python] 1507 궁금한 민호

플로이드 워셜. 최솟값이 최솟값끼리의 합이면 0 (direct한 경로가 아니라는 뜻) 최솟값끼리의 합보다 주어진 값이 크면? 최솟값이 아니라는 뜻. -1 출력. import sys input = sys.stdin.readline n = int(input()) matrix = [list(map(int, input().split())) for _ in range(n)] edge = [[1] * n for _ in range(n)] result = 0 for k in range(n): for i in range(n): for j in range(n): if i == j or j == k or i == k: continue # 합으로 표현되면 취하지 않는다.(그게 경로라는뜻) if matrix[i][j] == ..

[백준][Python] 16202 MST게임

사실 한번 0 출력되면 그뒤로 전부 0 출력하게끔 설계하는게 더 빠르지만 이 코드도 통과해서 그냥 냄. 크루스칼 import sys input = sys.stdin.readline def find(x): if parent[x] == x: return x parent[x] = find(parent[x]) return parent[x] def union(a,b): a = find(a) b = find(b) if level[a] >= level[b]: parent[b] = a if level[a] == level[b]: level[a] += 1 else: parent[a] = b N, M, K = map(int, input().split()) edges = [] for i in range(1,M+1): a, ..

[백준][Python] 10000 원 영역

라인 스위핑. 큰걸 기준으로 작은걸 감싸냐 안감싸냐 왼쪽이 겹치면 재귀로 오른쪽 이 겹쳐지는 원이 나올때까지 재귀(큰 원 안에 원이 몇개 겹치나 조사하기 위함) import sys from bisect import bisect_left input = sys.stdin.readline sys.setrecursionlimit(400000) def next_circle(cur_c,next_c): global cnt if circles[cur_c][1] == circles[next_c][1]: cnt += 1 return tmp = bisect_left(circles,(circles[next_c][1],)) if tmp == len(circles): return if circles[tmp][0]==circles..

[백준][Python] 10165 버스노선

라인 스위핑. 어떤 기준을 정하고 탈락은 어떻게 시킬 것이냐? 기준이 확장되어야한다면 넓은 범위를 먼저 조사해 기준으로 잡고 기준이 작아져야한다면 좁은 범위를 먼저 조사해 기준으로 잡아야 한다. import sys input = sys.stdin.readline N = int(input()) M = int(input()) path1 = [] path2 = [] visited = {i: False for i in range(1,M+1)} minA = int(1e12) maxB = -1 # 0을 거치는 최장 범위 minA ~ maxB를 구하고 모든 간선을 0을 거치는 것과 그렇지 않은것으로 나눔. for i in range(1,M+1): a, b = map(int, input().split()) if a

[백준][Python] 2170 선긋기

라인 스위핑 import sys input = sys.stdin.readline N = int(input()) lines = [list(map(int, input().split())) for _ in range(N)] lines.sort() answer = 0 left = right = 0 for start, end in lines: if not answer: answer = abs(end - start) left = start right = end continue # 여기가 스위핑문. 조건에서 벗어나는 라인은 조사 x if left = end: continue answer += abs(end - start) if right > start: answer -= abs(right - start) left =..