목록Algorithm (18)
토니의 연습장
# N 개 정수 입력 빠르게 하기# 대량의 정수를 입력 빠르게 하고자 하면 아래 재정의도 추가# input()을 재정의하여 더 빠른 sys.stdin.readline() 사용import sysinput = sys.stdin.readlineN = int(input())# 1. 한 줄에 하나씩 입력받기 일반적인 생각w1 = []for _ in range(N): w1.append(int(input()))# 2. 훨씬 빠른 방법 : list comprehension 이용w2 = [ int(input()) for _ in range(N)]# (참고) 띄어쓰기로 입력받을 때numbers1 = list(map(int, input().split()))
from itertools import combinationsN = int(input())w = []for _ in range(N): weight = int(input()) w.append(weight) # 오르차순 정렬w.sort()# k 개 선택tuples_list = [list(combinations(w,k)) for k in range(1,len(w)+1)]first_elements = [ [tuple[0] for tuple in tuples] for tuples in tuples_list]print(tuples_list)# 출력 & 최대 중량 저장max_weight = -1for i, sublist in enumerate(first_elements): print(f"tupl..
https://www.acmicpc.net/problem/2252from collections import dequeN, M = list(map(int, input().split()))adj = [[] for _ in range(N)]need_to_learn = [0]*Nfor _ in range(M): a,b = list(map(int, input().split())) adj[a-1].append(b-1) need_to_learn[b-1] += 1 # 위상 정렬queue = deque([]) # 처리 가능 목록for i in range(N): if need_to_learn[i] == 0: queue.append(i) # 리스트에 쌓아두고 하나씩 처리 위함 ..
https://www.acmicpc.net/problem/1463N = int(input())a = [0]*(N+1) # 길이 N+1, a[N] 참조 가능a[1] = 0for i in range(2, N+1): a[i] = 1+a[i-1] if i%3 == 0: a[i] = min(a[i], 1+a[i//3]) if i%2 == 0: a[i] = min(a[i], 1+a[i//2]) print(a[N]) https://www.acmicpc.net/problem/2579N = int(input())S = [0]*Nfor i in range(N): S[i] = int(input())A = [0]*NB = [0]*NA[0] = S[0]B[0] ..
https://www.acmicpc.net/problem/5525N = int(input())M = int(input())S = input()# 해쉬 값 계산 위한 준비mod = 1e9 + 7 # 10억 7 (큰 소수)po = [0]*Mpo[0] = 1for i in range(1,M): po[i]=po[i-1]*31 % mod# PnPn = "I"for i in range(N): # O가 N개 Pn += "OI" K = len(Pn)# Pn의 해쉬 값 계산Pn_hash = 0for i in range(K): Pn_hash *= 31 Pn_hash %= mod Pn_hash += ord(Pn[i])-ord('A')+1 Pn_hash %= mod# S[0:len..
https://www.acmicpc.net/problem/1417from queue import PriorityQueueN = int(input())P = [0]*Nfor i in range(N): P[i] = int(input()) pq = PriorityQueue() # default : 최솟값을 우선적으로 반환하는 큐for i in range(1,N): # 기호1번 제외 pq.put(-P[i]) # -를 붙임으로써 음수들로 들어가게끔 처리 if N == 1: print(0)else: count = 0 while True: max_value = -pq.get() # 음수중 최소값 반환에 앞에 -를 붙임으로써 절대값 기준 최댓값이 반환되게..
https://www.acmicpc.net/problem/2606N = int(input())M = int(input())adj = [[] for _ in range(N)] # 인접 리스트 [ [], [], ... , []]for _ in range(M): a, b = list(map(int, input().split())) adj[a-1].append(b-1) adj[b-1].append(a-1) check = [0]*Ncheck[0] = 1while True: new = False # if updated for i in range(N): if check[i] == 0: continue # chec..
https://www.acmicpc.net/problem/1068N = int(input())parent = list(map(int, input().split()))R = int(input())# 루트 노드 찾기for i in range(N): if parent[i]==-1: root = i break # 사라지는 노드 판별black = [0]*Nfor i in range(N): u = i while True: if u == R: black[i] = 1 break if u == root: break u = parent[u] # 자식 있는..