diff --git a/luna/class4-1/11053.py b/luna/class4-1/11053.py new file mode 100644 index 0000000..12d02ca --- /dev/null +++ b/luna/class4-1/11053.py @@ -0,0 +1,25 @@ +import sys + +def largest_A(n, A_s): + dp = [1] * n + + for i in range(1, n): + for j in range(i): + if A_s[j] < A_s[i]: + dp[i] = max(dp[i], dp[j] + 1) + + return max(dp) + +# 입력 처리 +input = sys.stdin.read +data = input().strip().splitlines() + +# 첫째 줄: 수열의 크기 +n = int(data[0]) + +# 둘째 줄: 수열을 이루고 있는 N개의 A_i들 +A_s = list(map(int, data[1].split())) + +# 출력 결과 +output = str(largest_A(n, A_s)) +sys.stdout.write(output + '\n') \ No newline at end of file diff --git a/luna/class4-1/1149.py b/luna/class4-1/1149.py new file mode 100644 index 0000000..2103c94 --- /dev/null +++ b/luna/class4-1/1149.py @@ -0,0 +1,13 @@ +num_house, *costs = map(int,open(0).read().split()) # num_house = 3, costs = [26, 40, 83, 49, 60, 57, 13, 89, 99] + +for i in range(3, len(costs)): # cost의 인덱스는 0부터 시작 + if i == 3: + costs[i] += min(costs[1], costs[2]) + elif i % 3 == 0: + costs[i] += min(costs[i-1], costs[i-2]) + elif i % 3 == 1: + costs[i] += min(costs[i-2], costs[i-4]) + else: + costs[i] += min(costs[i-4], costs[i-5]) + +print(min(costs[-1],costs[-2],costs[-3])) \ No newline at end of file diff --git a/luna/class4-1/11725.py b/luna/class4-1/11725.py new file mode 100644 index 0000000..63b5285 --- /dev/null +++ b/luna/class4-1/11725.py @@ -0,0 +1,20 @@ +from collections import deque + +N, *edges = map(int, open(0).read().split()) # edges = [1, 6, 6, 3, ...] +tree = [[] for _ in range(N + 1)] + +for a, b in zip(edges[::2], edges[1::2]): # 2씩 건너뛰며 zip [1, 6], [6, 3], ... + tree[a] += [b] # 연결된 노드 리스트 저장 + tree[b] += [a] + +parents = [0] * (N + 1) +queue = deque([1]) + +while queue: + current = queue.popleft() + for next_node in tree[current]: + if not parents[next_node]: + parents[next_node] = current + queue.append(next_node) + +print('\n'.join(map(str, parents[2:]))) # 2번 노드부터 출력 \ No newline at end of file diff --git a/luna/class4-1/15650.py b/luna/class4-1/15650.py new file mode 100644 index 0000000..fcfcd03 --- /dev/null +++ b/luna/class4-1/15650.py @@ -0,0 +1,17 @@ +import sys +from itertools import combinations + +def generate_combinations(n, m): + # 1부터 n까지의 수 중에서 m개를 선택하는 조합(c) 생성 + return combinations(range(1, n+1), m) + +# 입력 처리 +input = sys.stdin.read +data = input().strip().splitlines() + +# 첫째 줄: 자연수 N, M +N, M = map(int, data[0].split()) + +# 출력 결과 +output = '\n'.join(' '.join(map(str, combo)) for combo in generate_combinations(N, M)) +sys.stdout.write(output + '\n') \ No newline at end of file diff --git a/luna/class4-1/15654.py b/luna/class4-1/15654.py new file mode 100644 index 0000000..85c4c8d --- /dev/null +++ b/luna/class4-1/15654.py @@ -0,0 +1,19 @@ +import sys +from itertools import permutations + +def generate_permutations(numbers, m): + # n개의 수(=numbers) 중에서 m개를 선택하는 순열(p) 생성 + return sorted(permutations(numbers, m)) + +# 입력 처리 +input = sys.stdin.read +data = input().strip().splitlines() + +# 첫째 줄: 자연수 N, M (1 <= M <= N <= 8) +N, M = map(int, data[0].split()) +# 둘째 줄: N개의 수 (x <= 10,000) +nums = list(map(int, data[1].split())) + +# 출력 결과 +output = '\n'.join(' '.join(map(str, perm)) for perm in generate_permutations(nums, M)) +sys.stdout.write(output + '\n') \ No newline at end of file diff --git a/luna/class4-1/15663.py b/luna/class4-1/15663.py new file mode 100644 index 0000000..fbb3b6d --- /dev/null +++ b/luna/class4-1/15663.py @@ -0,0 +1,13 @@ +from itertools import permutations, starmap + +# open(0) : 표준 입력 받아오기 +NM, nums = [*open(0)] # NM = "3 1\n", nums = "4 4 2\n" + +# nums의 개행문자를 지우고 int로 변환해서 set에 넣은 후 정렬 +sorted_set = sorted({*permutations(map(int, nums.split()), int(NM[2]))}) # {(2, 4, 4), (4, 2, 4), (4, 4, 2)} + +for perm in sorted_set: + print(*perm) + +# 아래처럼 starmap 사용 가능 +# *starmap(print,sorted({*permutations(map(int, nums.split()), int(NM[2]))})), \ No newline at end of file diff --git a/luna/class4-1/1629.py b/luna/class4-1/1629.py new file mode 100644 index 0000000..421b220 --- /dev/null +++ b/luna/class4-1/1629.py @@ -0,0 +1,2 @@ +A, B, C = map(int, input().split()) +print(pow(A, B, C)) \ No newline at end of file diff --git a/luna/class4-1/1932.py b/luna/class4-1/1932.py new file mode 100644 index 0000000..4da1ac8 --- /dev/null +++ b/luna/class4-1/1932.py @@ -0,0 +1,22 @@ +import sys + +def max_sum_triangle(triangle, n): + for i in range(n-2, -1, -1): # 아래에서 위로 역순으로 이동, i의 범위는 n-1 ~ 0 + for j in range(i+1): # j의 범위는 i의 x 좌표의 +0 ~ +1 + triangle[i][j] += max(triangle[i+1][j], triangle[i+1][j+1]) # 아래 두 값 중 더 큰 값을 더함 + + return triangle[0][0] # 최종적으로 가장 위에 최대 합이 남음 + +# 입력 처리 +input = sys.stdin.read +data = input().strip().splitlines() + +# 첫째 줄: 삼각형의 크기 +n = int(data[0]) + +# 다음 n줄: 정수 삼각형의 정보 (각 줄의 값들, 공백 구분) +triangle = [list(map(int, line.split())) for line in data[1:]] + +# 출력 결과 +output = str(max_sum_triangle(triangle, n)) +sys.stdout.write(output + '\n') \ No newline at end of file diff --git a/luna/class4-1/1991.py b/luna/class4-1/1991.py new file mode 100644 index 0000000..00d51ab --- /dev/null +++ b/luna/class4-1/1991.py @@ -0,0 +1,32 @@ +N, *lines = open(0).read().splitlines() # 줄마다 읽어옴, n = 7, lines = ['A B C', ...] +data = {a: (b, c) for a, b, c in (line.split() for line in lines)} # {'A': ('B', 'C'), ...} + +def order_n1(i): # 전위 순회 + print(i, end='') # 루트 출력 + left, right = data[i] + if left != '.': + order_n1(left) # 왼쪽 자식 순회 + if right != '.': + order_n1(right) # 오른쪽 자식 순회 + +def order_n2(i): # 중위 순회 + left, right = data[i] + if left != '.': + order_n2(left) # 왼쪽 자식 순회 + print(i, end='') # 루트 출력 + if right != '.': + order_n2(right) # 오른쪽 자식 순회 + +def order_n3(i): # 후위 순회 + left, right = data[i] + if left != '.': + order_n3(left) # 왼쪽 자식 순회 + if right != '.': + order_n3(right) # 오른쪽 자식 순회 + print(i, end='') # 루트 출력 + +order_n1('A') +print() +order_n2('A') +print() +order_n3('A') \ No newline at end of file diff --git a/luna/class4-1/9465.py b/luna/class4-1/9465.py new file mode 100644 index 0000000..07c17ed --- /dev/null +++ b/luna/class4-1/9465.py @@ -0,0 +1,17 @@ +N, *data = [*open(0)] +lines = [list(map(int, _.split())) for _ in data] + +for i in range(int(N)): + num = lines[3*i][0] + top_row = lines[3*i + 1] + bottom_row = lines[3*i + 2] + + if num > 1: + top_row[1] += bottom_row[0] + bottom_row[1] += top_row[0] + + for j in range(2, num): + top_row[j] += max(bottom_row[j-1], bottom_row[j-2]) + bottom_row[j] += max(top_row[j-1], top_row[j-2]) + + print(max(top_row[-1], bottom_row[-1])) \ No newline at end of file