본문 바로가기

Algorithm/BOJ133

[백준] 1365번 - 꼬인 전깃줄 (파이썬) # 꼬인 전깃줄 import bisect N = int(input()) arr = list(map(int, input().split())) result = [arr[0]] for i in range(1, len(arr)): index = bisect.bisect_left(result, arr[i]) if index == len(result): result.append(arr[i]) else: result[index] = arr[i] print(len(arr)-len(result)) 이분탐색, bisect 1. 입력을 받는다. 2. result 리스트 선언 3. arr의 각 원소마다 - result가 비었으면 append - bisect_left로 result에 x가 들어갈 인덱스를 구한다. - index.. 2022. 4. 5.
[백준] 13905번 - 세부 (파이썬) # 세부 import heapq import sys input = sys.stdin.readline N, M = map(int, input().split()) start, end = map(int, input().split()) arr = [[] for _ in range(N+1)] for _ in range(M): a, b, weight = map(int, input().split()) arr[a].append((weight, b)) arr[b].append((weight, a)) visited = [0] * (N+1) def dijkstra(): q = [] heapq.heappush(q, (-sys.maxsize, start)) visited[start] = sys.maxsize while q: c.. 2022. 4. 5.
[백준] 2866번 - 문자열 잘라내기 (파이썬) # 문자열 잘라내기 from collections import defaultdict R, C = map(int, input().split()) arr = [list(input()) for _ in range(R)] result = 0 start, end = 0, R-1 while start 2022. 4. 4.
[백준] 2295번 - 세 수의 합 (파이썬) # 세 수의 합 import sys input = sys.stdin.readline N = int(input()) arr = [int(input()) for _ in range(N)] arr.sort() arr_sum = set() for x in arr: for y in arr: arr_sum.add(x+y) def check(): global answer for i in range(N-1, -1, -1): for j in range(i+1): if arr[i]-arr[j] in arr_sum: answer = arr[i] return answer = 0 check() print(answer) 풀이 x + y + z = k 일 때, x + y = k - z 임을 이용해서 풀었다. 1. arr_sum :.. 2022. 4. 3.
[백준] 17396번 - 백도어 (파이썬) # 백도어 import heapq import sys input = sys.stdin.readline N, M = map(int, input().split()) cant_go = list(map(int, input().split())) cant_go[-1] = 0 arr = [[]for _ in range(N)] for _ in range(M): a, b, cost = map(int, input().split()) arr[a].append((cost, b)) arr[b].append((cost, a)) dist = [sys.maxsize] * N def dijkstra(): q = [] heapq.heappush(q, (0, 0)) dist[0] = 0 while q: cost, node = heapq... 2022. 4. 2.
[백준] 5972번 - 택배 배송 (파이썬) # 택배 배송 import heapq import sys input = sys.stdin.readline N, M = map(int, input().split()) arr = [[]for _ in range(N+1)] for _ in range(M): a, b, cost = map(int, input().split()) arr[a].append((cost, b)) arr[b].append((cost, a)) def dijkstra(): q = [] heapq.heappush(q, (0, 1)) total = [sys.maxsize] * (N+1) total[1] = 0 while q: cost, node = heapq.heappop(q) if node == N: return total[node] if t.. 2022. 4. 1.
[백준] 1719번 - 택배 (파이썬) # 택배 import sys input = sys.stdin.readline n, m = map(int, input().split()) arr = [[sys.maxsize] * (n+1) for _ in range(n+1)] for i in range(1, n+1): arr[i][i] = 0 for _ in range(m): x, y, cost = map(int, input().split()) arr[x][y] = cost arr[y][x] = cost result = [[j for j in range(n+1)] for i in range(n+1)] for i in range(1, n+1): result[i][i] = '-' for k in range(1, n+1): for i in range(1, n+.. 2022. 3. 31.