practivceAlgorithm 570

[SWEA][Python] 1220 magnetic

문제 보자마자 무지성 구현. 한칸씩 이동 이동 멈추면 반복문 종료. 다른 사람들은 0 빼고 1,2만 새 배열에 담아서 switch바꾸는 형식으로 풀었더라. 상태1에서 2를 만나면 1더하는 식으로. import sys sys.stdin = open('input.txt') def move_block(): check = 0 new_struck = 0 for i in range(N): for j in range(N): if not i == 99 and matrix[i][j] == 1: if not matrix[i+1][j]: matrix[i][j], matrix[i+1][j] = matrix[i+1][j], matrix[i][j] check = 1 elif matrix[i+1][j] == 1: matrix[i]..

[백준][Python] 21939 문제 추천 시스템

핵심은 이미 푼 문제를 각 힙에서 제거하는 매커니즘. import sys input = sys.stdin.readline from heapq import heappop,heappush from collections import defaultdict N = int(input()) min_heap = [] max_heap = [] in_list = defaultdict(bool) for _ in range(N): P, L = map(int, input().split()) heappush(min_heap,[L,P]) heappush(max_heap,[-L,-P]) in_list[P] = True M = int(input()) for _ in range(M): command = input().split() if..

[백준][Python] 22867 종점

시간정보에 대한 전처리가 귀찮은 문제. 저게 최선인가 싶긴 하다. 나머지로직은 강의실 배정, 방배정 문제와 동일. import sys input = sys.stdin.readline from heapq import heappush, heappop N = int(input()) buses = [] for _ in range(N): arrive, leave = input().split() a = int(''.join((''.join(arrive.split(':'))).split('.'))) b = int(''.join((''.join(leave.split(':'))).split('.'))) buses.append([a,b]) buses.sort() heap = [] heappush(heap,buses[0][..

[백준][Python] 22941 RPG마스터 오명진

하나씩 때리면 시간초과고 마왕이 끔살나는 기준에 따라 죽는 회차가 다르니 그거만 계산해서 용사 죽는 회차랑 비교해주면 된다. import sys input = sys.stdin.readline from math import ceil h_hp, h_atk, d_hp, d_atk = map(int, input().split()) P, S = map(int, input().split()) # h_hp d_atk로 나눈 몫 올림값이 용사 사망 cnt h_death_cnt = ceil(h_hp/d_atk) # d_hp - P 를 h_atk로 때린 나머지에 P를 더한 값이 h_atk보다 작을경우 d_hp - P를 h_atk로 나눈 몫+1에 끔살 if (d_hp-P)%h_atk and (d_hp-P)%h_atk + ..

[백준][Python] 2141 우체국

이분탐색으로 무게중심 찾고 그 좌우에 있는 마을만 거리비교해서 최솟값으로 가져감. 둘 거리같으면 왼쪽꺼 가져감. import sys input = sys.stdin.readline from bisect import bisect_left def cal_dist(position): dist = 0 for town, population in arr: dist += abs(position-town)*population return dist N = int(input()) arr = [tuple(map(int, input().split())) for _ in range(N)] left = -1000000000 right = 1000000000 answer = 0 while left

[백준][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:..