2024/07/25 12

항해99 리부트코스 알고리즘 2주 2일차 백준 14235 크리스마스 선물

https://www.acmicpc.net/problem/14235   풀이import heapqimport sysinput = sys.stdin.readlinedef main(): n = int(input().strip()) # 명령의 개수 N을 입력받음 max_heap = [] # 빈 리스트를 최대 힙으로 사용 for _ in range(n): command = list(map(int, input().strip().split())) if command[0] == 0: # 선물을 받아야 하는 경우 if max_heap: # 힙이 비어있지 않으면 가장 큰 선물 출력 후 제거 ..

Algorithm/백준 2024.07.25

항해99 리부트코스 알고리즘 2주 2일차 백준 1927 최소 힙

https://www.acmicpc.net/problem/1927    import sys import heapq input = sys.stdin.readlinedef main(): n = int(input().strip()) min_heap = [] # 빈 리스트를 최소 힙으로 사용 for _ in range(n): x = int(input().strip()) # 각 명령을 입력받음 if x == 0: if min_heap: # 힙이 비어있지 않으면 최소 값을 출력하고 힙에서 제거 print(heapq.heappop(min_heap)) else: ..

Algorithm/백준 2024.07.25

항해99 리부트코스 알고리즘 2주 2일차 백준 19638 센티와 마법의 뿅망치

https://www.acmicpc.net/problem/19638    풀이  주어진 거인의 키들을 최대 힙(max heap)으로 관리최대 힙을 사용하여 가장 큰 거인을 찾아 뿅망치로 줄임이를 T번 반복하거나, 모든 거인의 키가 H이하가 되면 중지 import heapqimport sysinput = sys.stdin.readlinedef solve(n, h, t, giants): # 거인들의 키를 최대 힙으로 변환 (음수로 저장하여 최대 힙처럼 동작하게 함) max_heap = [-giant for giant in giants] heapq.heapify(max_heap) count = 0 # 뿅망치 사용 횟수 카운트 for _ in range(t): # 최대 T번..

Algorithm/백준 2024.07.25

항해99 리부트코스 알고리즘 2주 1일차 백준 1406 에디터

https://www.acmicpc.net/problem/1406 import sysinput = sys.stdin.read# 초기 문자열과 명령어 입력 받기data = input().splitlines()initial_string = data[0]commands = data[1:]# 왼쪽 스택과 오른쪽 스택 사용left_stack = list(initial_string) # 초기 문자열을 왼쪽 스택에 넣음right_stack = []# 명령어 처리for command in commands: if command.startswith('L'): if left_stack: right_stack.append(left_stack.pop()) # 왼쪽 스택에서 오른쪽 스택으..

Algorithm/백준 2024.07.25