본문 바로가기
  • KEEP HUSTLE!

전체 글73

[백준 11051] 이항 계수 2 - Python(파이썬) import sys N, K = map(int, sys.stdin.readline().split()) K = min(K, N-K) answer = 1 for i in range(N, N-K, -1): answer *= i answer //= (N+1 - i) print(answer % 10007) 2021. 3. 31.
[백준 2470] 두 용액 - Python(파이썬) 투 포인터를 사용한 코드 import sys N = int(sys.stdin.readline().strip()) arr = sorted(list(map(int, sys.stdin.readline().split()))) s, e = 0, N-1 my_max = float('inf') answer = [] while s abs(temp): my_max = abs(temp) answer = [arr[s], arr[e]] if abs(arr[s]) < abs(arr[e]): e -= 1 else: s += 1 print(*answer) 2021. 3. 31.
[백준 3273] 두 수의 합 - Python(파이썬) 투포인터를 사용한 코드 import sys n = int(sys.stdin.readline().strip()) arr = sorted(list(map(int, sys.stdin.readline().split()))) key = int(sys.stdin.readline().strip()) answer = 0 s, e = 0, n-1 while s < e: my_sum = arr[s] + arr[e] if my_sum == key: answer += 1 e -= 1 elif my_sum < key: s += 1 else: e -= 1 print(answer) 2021. 3. 31.
[백준 1956] 운동 - Python(파이썬) from heapq import heappush, heappop import sys # 다익스트라 구현 def solution(x): check = [inf] * (V + 1) temp = [(0, x)] while temp: # w: 거리, idx: 해당 인덱스 w, idx = heappop(temp) # 인덱스가 x라는 것은 가장 짧게 한 사이클을 돌았다는 것을 의미 if x == idx and w != 0: break if check[idx] >= w: for i, d in dist[idx]: d += w if check[i] > d: check[i] = d heappush(temp, (d, i)) return check[x] V, E = map(int, sys.stdin.readline().spl.. 2021. 3. 27.
[백준 10217] KCM Travel - Python(파이썬) 다익스트라와 냅색을 활용한 코드 from heapq import heappop, heappush import sys def solution(): check = [[float('inf')] * (M+1) for _ in range(N+1)] temp = [(0, 0, 1)] while temp: # d: 거리, c: 비용, idx: 해당 인덱스 d, c, idx = heappop(temp) if check[idx][c] < d: continue # 원하는 인덱스면, 해당 거리 리턴! if idx == N: return d # k: 해당 인덱스, l: 비용, m: 거리 for k, l, m in dist[idx]: l += c m += d # 합 비용이 예산 초과거나 합 거리가 check의 거리 이상일 경우.. 2021. 3. 27.
[백준 11404] 플로이드 - Python(파이썬) import sys n = int(sys.stdin.readline().strip()) m = int(sys.stdin.readline().strip()) arr = [[float('inf')] * n for _ in range(n)] for _ in range(m): a, b, c = map(int, sys.stdin.readline().split()) arr[a-1][b-1] = min(arr[a-1][b-1], c) # 모든 하나하나 돌면서 해당 노드를 거쳤을 때를 비교 for k in range(n): arr[k][k] = 0 for i in range(n): for j in range(n): arr[i][j] = min(arr[i][j], arr[i][k] + arr[k][j]) for t in.. 2021. 3. 27.