본문 바로가기

전체 글

(208)
연료 채우기 [백준 1826] https://www.acmicpc.net/problem/1826 import heapqimport sys# input = sys.stdin.read# data = input().splitlines()N = int(input()) # 주유소 개수stations = []for i in range(1, N + 1): # a, b = map(int, data[i].split()) a, b = map(int, input().split()) stations.append((a, b))L, P = map(int, input().split()) # 마을까지 거리, 현재 연료stations.sort()max_heap = []fuel = Pcount = 0index = 0 while fuel
파일 합치기3 [백준 13975] https://www.acmicpc.net/problem/13975 import heapqt = int(input()) # 테스트 케이스 수for _ in range(t): k = int(input()) # 배열의 길이 arr = list(map(int, input().split())) heapq.heapify(arr) total = 0 while len(arr) > 1: a1 = heapq.heappop(arr) a2 = heapq.heappop(arr) total += (a1 + a2) heapq.heappush(arr, (a1 + a2)) print(total)
멀티탭 스케줄링 [백준 1700] https://www.acmicpc.net/problem/1700 # 입력 받기n, k = map(int, input().split()) # n: 멀티탭 구멍 수, k: 사용 순서 길이order = list(map(int, input().split()))multitap = []swap_count = 0for i in range(k ): curr = order[i] for i in range(k): current = order[i] if current in multitap: continue if len(multitap) farthest: farthest = next_use index_to_remove = j multitap[..
흙길 보수하기 [백준 1911] https://www.acmicpc.net/problem/1911# 입력 받기import sysinput = sys.stdin.read().splitlines()N, L = map(int, input[0].split())puddles = []for i in range(1, N + 1): start, end = map(int, input[i].split()) puddles.append((start, end))puddles.sort()plank_count = 0cover_end = 0 for start, end in puddles: # 만약 이미 덮여있는 범위보다 시작이 작으면, 덮여 있는 부분 이후부터 시작 if cover_end >= start: start = cover..
다이어트 [백준 1484] https://www.acmicpc.net/problem/1484 import sysG = int(sys.stdin.readline().strip())answers = []low = 1high = 2 while True: diff = high*high - low*low # (diff == G)이면 후보가 됨 if diff == G: answers.append(high) # 현재 몸무게 = high if diff >= G: low += 1 else: high += 1 if low >= high: break if high > 100000: break# 결과 출력if not answers: pri..
전쟁-전투[백준 1303] https://www.acmicpc.net/problem/1303  from collections import deque n, m = map(int, input().split())arr = [list(input().strip()) for _ in range(m)]visit = [[0] * n for _ in range(m)]b, w = [], []dx, dy = [1, -1, 0, 0], [0, 0, 1, -1]def bfs(x, y, char): q = deque() q.append((x, y)) cnt = 1 visit[x][y] = 1 while q: x, y = q.popleft() for i in range(4): nx,..
양치기 꿍[백준 3187] https://www.acmicpc.net/problem/3187from collections import dequeimport sysinput = sys.stdin.readdata = input().split("\n")R, C = map(int, data[0].split())graph = [list(data[i]) for i in range(1, R+1)]dx = [-1, 1, 0, 0]dy = [0, 0, -1, 1]visited = [[False] * C for _ in range(R)]def bfs(x, y): queue = deque([(x, y)]) visited[x][y] = True sheep, wolf = 0, 0 # 현재 영역의 양과 늑대 수 while queue..
텔레포트 정거장[백준 18232] https://www.acmicpc.net/problem/18232   from collections import dequedef bfs(N, S, E, teleport): visited = [-1] * (N + 1) # 방문 여부 및 시간 기록 queue = deque([S]) visited[S] = 0 # 시작점은 0초 while queue: current = queue.popleft() # 목표 지점에 도달한 경우 if current == E: return visited[current] # X+1로 이동 if current + 1 = 1 and visited[current - 1] == -1:..