프로그래밍/Algorithm 105

백준 14502 연구소 파이썬

www.acmicpc.net/problem/14502 14502번: 연구소 인체에 치명적인 바이러스를 연구하던 연구소에서 바이러스가 유출되었다. 다행히 바이러스는 아직 퍼지지 않았고, 바이러스의 확산을 막기 위해서 연구소에 벽을 세우려고 한다. 연구소는 크 www.acmicpc.net import copy import sys import itertools from collections import deque if __name__ == '__main__': N, M = map(int, sys.stdin.readline().split()) graph = [] for _ in range(N): graph.append(list(map(int, sys.stdin.readline().split()))) all_wa..

백준 1260 DFS와 BFS

www.acmicpc.net/problem/1260 1260번: DFS와 BFS 첫째 줄에 정점의 개수 N(1 ≤ N ≤ 1,000), 간선의 개수 M(1 ≤ M ≤ 10,000), 탐색을 시작할 정점의 번호 V가 주어진다. 다음 M개의 줄에는 간선이 연결하는 두 정점의 번호가 주어진다. 어떤 두 정점 사 www.acmicpc.net from collections import deque N, M, V = map(int, input().split()) graph = [[] for _ in range(N+1)] for _ in range(M): a, b = map(int, input().split()) graph[a].append(b) graph[a].sort() graph[b].append(a) graph..

백준 1826 연료 채우기 파이썬

www.acmicpc.net/problem/1826 1826번: 연료 채우기 첫째 줄에 주유소의 개수 N(1 ≤ N ≤ 10,000)가 주어지고 두 번째 줄부터 N+1번째 줄 까지 주유소의 정보가 주어진다. 주유소의 정보는 두개의 정수 a,b로 이루어 져 있는데 a(1 ≤ a ≤ 1,000,000)는 성경 www.acmicpc.net import heapq N = int(input()) gas_stations = [] for _ in range(N): # 주유소까지의 거리, 채울수 있는 연료의 양 a, b = map(int, input().split()) heapq.heappush(gas_stations, (a, -b)) town_distance, my_gas = map(int, input().split..

백준 5545 최고의피자 파이썬

www.acmicpc.net/problem/5545 5545번: 최고의 피자 첫째 줄에 토핑의 종류의 수 N(1 ≤ N ≤ 100)이 주어진다. 둘째 줄에는 도우의 가격 A와 토핑의 가격 B가 주어진다. (1 ≤ A, B ≤ 1000) 셋째 줄에는 도우의 열량 C가 주어진다. (1 ≤ C ≤ 10000) 다음 줄 www.acmicpc.net N = int(input()) A, B = map(int, input().split()) C = int(input()) D = [] for _ in range(N): D.append(int(input())) D.sort(reverse=True) current_price = A current_calories = C max_calories = C / A for topin..

백준 12018 Yonsei TOTO 파이썬

www.acmicpc.net/problem/12018 12018번: Yonsei TOTO 연세대학교 수강신청이 얼마 전부터 바뀌어, 마일리지 제도로 바뀌었다. 이 제도는 각각의 학생들에게 마일리지를 주어 듣고 싶은 과목에 마일리지를 과목당 1~36을 분배한다. 그리고 모두 분배 www.acmicpc.net import heapq ans = 0 n, m = map(int, input().split()) temp = [] for _ in range(n): P, L = map(int, input().split()) mileages = list(map(int, input().split())) heapq.heapify(mileages) num = L - P # 수강미달인 과목 if num > 0: heapq.he..

백준 16120 PPAP 파이썬

www.acmicpc.net/problem/16120 16120번: PPAP 첫 번째 줄에 문자열이 주어진다. 문자열은 대문자 알파벳 P와 A로만 이루어져 있으며, 문자열의 길이는 1 이상 1,000,000 이하이다. www.acmicpc.net word = input() if word == 'P' or word == 'PPAP': print('PPAP') else: stack = [] ppap = ['P','P','A','P'] for i in word: stack.append(i) if stack[-4:] == ppap: stack.pop() stack.pop() stack.pop() if stack == ppap or stack == ['P']: print('PPAP') else: print('NP..

백준 11509 풍선 맞추기 파이썬

www.acmicpc.net/problem/11509 11509번: 풍선 맞추기 첫 번째 예제 에서 [5,4,3] 을 터트리고 [2,1]을 터트리면 모든 풍선을 터트릴 수 있으므로 최소한 2개의 화살을 필요로 한다. www.acmicpc.net 약간 애를 먹었다. 구현 핵심 아이디어를 초반에 생각했다가 다른 방법으로 풀었는데 시간초과가 발생했다. 구현 방식을 O(N) 으로 변경해서 다시 구현했는데 이번엔 실수로 한참 헤맸다. N = int(input()) ballons = list(map(int, input().split())) flag = [0] * 1000001 ans = 0 for i in range(len(ballons)): if flag[ballons[i]] == 0: ans+=1 flag[ba..

백준 13417 카드 문자열 파이썬

www.acmicpc.net/problem/13417 13417번: 카드 문자열 입력 데이터는 표준 입력을 사용한다. 입력은 T개의 테스트 데이터로 구성된다. 입력의 첫째 줄에 테스트 케이스의 개수를 나타내는 자연수 T가 주어진다. 각각의 테스트 케이스의 첫째 줄에 처 www.acmicpc.net T = int(input()) for _ in range(T): N = int(input()) A = list(map(str, input().split())) ans = [A[0]] for i in range(1, len(A)): left = ans[0] right = ans[-1] if A[i]

백준 1105 팔 파이썬

www.acmicpc.net/problem/1105 1105번: 팔 첫째 줄에 L과 R이 주어진다. L은 2,000,000,000보다 작거나 같은 자연수이고, R은 L보다 크거나 같고, 2,000,000,000보다 작거나 같은 자연수이다. www.acmicpc.net L, R = map(int, input().split()) ans = 0 if len(str(L)) != len(str(R)): print(0) else: L_str = str(L) R_str = str(R) if L_str[0] != R_str[0]: print(0) else: if L_str[0] == '8': ans+=1 for i in range(1, len(L_str)): if L_str[i] != R_str[i]: break el..

백준 4889 안정적인 문자열

www.acmicpc.net/problem/4889 4889번: 안정적인 문자열 입력은 여러 개의 데이터 세트로 이루어져 있다. 각 데이터 세트는 한 줄로 이루어져 있다. 줄에는 여는 괄호와 닫는 괄호만으로 이루어진 문자열이 주어진다. 문자열의 길이가 2000을 넘는 경우 www.acmicpc.net cnt = 0 while True: ans = 0 A = [] s = input() if s.startswith('-'): break for i in s: if i == '{': A.append(i) else: if A: A.pop() else: A.append('{') ans+=1 ans+= len(A) // 2 if len(A) > 1 else len(A) cnt+=1 print(f'{cnt}. {ans..