├── Exams ├── coding_test_A.pdf ├── coding_test_B.pdf ├── coding_test_C.pdf ├── coding_test_D.pdf ├── coding_test_E.pdf └── solutions │ ├── A_1.py │ ├── A_2.py │ ├── A_3.py │ ├── B_1.py │ ├── B_2.py │ ├── B_3.py │ ├── C_1.py │ ├── C_2.py │ ├── C_3.py │ ├── D_1.py │ ├── D_2.py │ ├── D_3.py │ ├── E_1.py │ ├── E_2.py │ └── E_3.py ├── Java369 ├── 1.java ├── 2.java ├── 3.java ├── 4.java ├── 5.java ├── 6.java ├── 7.java ├── 8.java └── README.md ├── Notes ├── [01] CHAPTER 01. 기본 자료구조 - 기초 문제풀이.pdf ├── [02] CHAPTER 01. 기본 자료구조 - 핵심 유형 문제풀이.pdf ├── [03] CHAPTER 02. 고급 자료구조 - 핵심 유형 문제풀이.pdf ├── [04] CHAPTER 03. 기본 정렬 알고리즘 - 기초 문제풀이.pdf ├── [05] CHAPTER 03. 기본 정렬 알고리즘 - 핵심 유형 문제풀이.pdf ├── [06] CHAPTER 04. 재귀 호출 - 핵심 유형 문제풀이.pdf ├── [07] CHAPTER 05. 고급 정렬 알고리즘 - 핵심 유형 문제풀이.pdf ├── [08] CHAPTER 06. 기본 탐색 알고리즘 - 기초 문제풀이.pdf ├── [09] CHAPTER 06. 기본 탐색 알고리즘 - 핵심 유형 문제풀이.pdf ├── [10] CHAPTER 07. 고급 탐색 알고리즘 - 기초 문제풀이.pdf ├── [11] CHAPTER 07. 고급 탐색 알고리즘 - 핵심 유형 문제풀이.pdf ├── [12] CHAPTER 08. 동적 프로그래밍 - 기초 문제풀이.pdf ├── [13] CHAPTER 08. 동적 프로그래밍 - 핵심 유형 문제풀이.pdf ├── [14] CHAPTER 09. 그래프 기본 탐색 알고리즘 - 기초 문제풀이.pdf ├── [15] CHAPTER 09. 그래프 기본 탐색 알고리즘 - 핵심 유형 문제풀이.pdf ├── [16] CHAPTER 10. 그래프 고급 탐색 알고리즘 - 핵심 유형 문제풀이.pdf ├── [17] CHAPTER 11. 탐욕 알고리즘 - 기초 문제풀이.pdf ├── [18] CHAPTER 11. 탐욕 알고리즘 - 핵심 유형 문제풀이.pdf └── [19] CHAPTER 12. 백트래킹 - 핵심 유형 문제풀이.pdf ├── README.md └── Solutions ├── [01]_1.cpp ├── [01]_1.java ├── [01]_1.py ├── [01]_2.cpp ├── [01]_2.java ├── [01]_2.py ├── [02]_1.cpp ├── [02]_1.java ├── [02]_1.py ├── [02]_2.cpp ├── [02]_2.java ├── [02]_2.py ├── [02]_3.cpp ├── [02]_3.java ├── [02]_3.py ├── [03]_1.java ├── [03]_1.py ├── [03]_2.cpp ├── [03]_2.java ├── [03]_2.py ├── [03]_3.cpp ├── [03]_3.py ├── [04]_1.cpp ├── [04]_1.java ├── [04]_1.py ├── [04]_2.cpp ├── [04]_2.java ├── [04]_2.py ├── [05]_1.cpp ├── [05]_1.java ├── [05]_1.py ├── [05]_2.cpp ├── [05]_2.java ├── [05]_2.py ├── [05]_3.cpp ├── [05]_3.java ├── [05]_3.py ├── [06]_1.cpp ├── [06]_1.java ├── [06]_1.py ├── [06]_2.cpp ├── [06]_2.java ├── [06]_2.py ├── [06]_3.py ├── [07]_1.cpp ├── [07]_1.java ├── [07]_1.py ├── [07]_2.cpp ├── [07]_2.java ├── [07]_2.py ├── [08]_1.cpp ├── [08]_1.java ├── [08]_1.py ├── [08]_2.cpp ├── [08]_2.java ├── [08]_2.py ├── [08]_3.cpp ├── [08]_3.java ├── [08]_3.py ├── [08]_4.cpp ├── [08]_4.java ├── [08]_4.py ├── [08]_5.cpp ├── [08]_5.java ├── [08]_5.py ├── [09]_1.cpp ├── [09]_1.java ├── [09]_1.py ├── [09]_2.py ├── [10]_1.py ├── [10]_2.py ├── [11]_1.cpp ├── [11]_1.java ├── [11]_1.py ├── [11]_2.cpp ├── [11]_2.java ├── [11]_2.py ├── [11]_3.py ├── [12]_1.cpp ├── [12]_1.java ├── [12]_1.py ├── [12]_2.cpp ├── [12]_2.java ├── [12]_2.py ├── [12]_3.cpp ├── [12]_3.java ├── [12]_3.py ├── [13]_1.cpp ├── [13]_1.java ├── [13]_1.py ├── [13]_2.cpp ├── [13]_2.java ├── [13]_2.py ├── [13]_3.py ├── [14]_1.cpp ├── [14]_1.java ├── [14]_1.py ├── [14]_2.cpp ├── [14]_2.java ├── [14]_2.py ├── [15]_1.cpp ├── [15]_1.java ├── [15]_1.py ├── [15]_2.cpp ├── [15]_2.java ├── [15]_2.py ├── [15]_3.cpp ├── [15]_3.java ├── [15]_3.py ├── [16]_1.cpp ├── [16]_1.py ├── [16]_2.cpp ├── [16]_2.py ├── [16]_3.cpp ├── [16]_3.py ├── [17]_1.cpp ├── [17]_1.java ├── [17]_1.py ├── [17]_2.cpp ├── [17]_2.java ├── [17]_2.py ├── [17]_3.cpp ├── [17]_3.java ├── [17]_3.py ├── [17]_4.cpp ├── [17]_4.java ├── [17]_4.py ├── [18]_1.cpp ├── [18]_1.java ├── [18]_1.py ├── [18]_2.cpp ├── [18]_2.java ├── [18]_2.py ├── [18]_3.cpp ├── [18]_3.java ├── [18]_3.py ├── [19]_1.cpp ├── [19]_1.py ├── [19]_2.py └── [19]_3.py /Exams/coding_test_A.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ndb796/Fast_Campus_Algorithm_Lecture_Notes/d5948b7e9897219bba1cdd2fc68e36bacc03051a/Exams/coding_test_A.pdf -------------------------------------------------------------------------------- /Exams/coding_test_B.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ndb796/Fast_Campus_Algorithm_Lecture_Notes/d5948b7e9897219bba1cdd2fc68e36bacc03051a/Exams/coding_test_B.pdf -------------------------------------------------------------------------------- /Exams/coding_test_C.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ndb796/Fast_Campus_Algorithm_Lecture_Notes/d5948b7e9897219bba1cdd2fc68e36bacc03051a/Exams/coding_test_C.pdf -------------------------------------------------------------------------------- /Exams/coding_test_D.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ndb796/Fast_Campus_Algorithm_Lecture_Notes/d5948b7e9897219bba1cdd2fc68e36bacc03051a/Exams/coding_test_D.pdf -------------------------------------------------------------------------------- /Exams/coding_test_E.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ndb796/Fast_Campus_Algorithm_Lecture_Notes/d5948b7e9897219bba1cdd2fc68e36bacc03051a/Exams/coding_test_E.pdf -------------------------------------------------------------------------------- /Exams/solutions/A_1.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | a = list(map(int, input().split())) 3 | a.sort() 4 | 5 | # Median(가운데) 값을 출력합니다. 6 | print(a[(n - 1) // 2]) -------------------------------------------------------------------------------- /Exams/solutions/A_2.py: -------------------------------------------------------------------------------- 1 | n, k = map(int, input().split()) 2 | a = list(map(int, input().split())) 3 | result = 0 4 | 5 | def dfs(x): 6 | global result 7 | # n보다 작거나 같은 자연수에 대해서만 확인 8 | if x > n: 9 | return 10 | # K의 원소로만 구성된 가장 큰 수를 저장 11 | result = max(x, result) 12 | for i in a: 13 | # K의 원소로만 구성된 모든 수를 탐색 14 | dfs(x * 10 + i) 15 | 16 | dfs(0) 17 | print(result) -------------------------------------------------------------------------------- /Exams/solutions/A_3.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | 3 | n, m = map(int, input().split()) 4 | x, y = map(int, input().split()) 5 | 6 | # 모든 상대편 말의 위치를 저장 7 | enemy = [] 8 | for i in range(m): 9 | a, b = map(int, input().split()) 10 | enemy.append((a, b)) 11 | 12 | # 보드 전체를 -1으로 초기화 13 | board = [[-1] * (n + 1) for _ in range(n + 1)] 14 | # 나이트가 이동할 수 있는 8가지의 위치 15 | dx = [-2, -2, -1, -1, 1, 1, 2, 2] 16 | dy = [-1, 1, -2, 2, -2, 2, -1, 1] 17 | 18 | # 현재 위치를 큐에 삽입 19 | q = deque() 20 | q.append((x, y)) 21 | # 현재 위치에 대한 최소 이동 수는 0 22 | board[x][y] = 0 23 | 24 | # 보드의 모든 위치에 대하여 최소 이동 수를 계산 25 | while q: 26 | x, y = q.popleft() 27 | # 8가지 위치를 각각 확인 28 | for i in range(8): 29 | nx = x + dx[i] 30 | ny = y + dy[i] 31 | # 해당 위치로 이동할 수 있는 경우 32 | if 1 <= nx and nx <= n and 1 <= ny and ny <= n: 33 | # 방문한 적 없다면, 그 위치로의 최소 이동 수 계산 34 | if board[nx][ny] == -1: 35 | board[nx][ny] = board[x][y] + 1 36 | q.append((nx, ny)) 37 | 38 | # 모든 상대편 말까지의 최소 이동 수 출력 39 | for a, b in enemy: 40 | print(board[a][b], end=' ') -------------------------------------------------------------------------------- /Exams/solutions/B_1.py: -------------------------------------------------------------------------------- 1 | n, k = map(int, input().split()) 2 | data = list(map(int, input().split())) 3 | 4 | course = [] 5 | for i in range(n): 6 | # (해당 코스의 번호, 해당 코스의 길이)를 입력 7 | course.append((i + 1, data[i])) 8 | # 왕복 코스를 직선 형태로 표현 9 | course.extend(list(reversed(course))) 10 | 11 | # 코스의 길이를 앞에서부터 빼내기 12 | i = 0 13 | while True: 14 | k -= course[i][1] 15 | # 지나고 있는 코스 출력 및 종료 16 | if k < 0: 17 | print(course[i][0]) 18 | break 19 | i += 1 -------------------------------------------------------------------------------- /Exams/solutions/B_2.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | data = list(map(int, input().split())) 3 | # 순서를 바꾸어 '최장 증가 부분 수열' 문제로 변환 4 | data.reverse() 5 | 6 | # LCS 알고리즘 수행 7 | dp = [1] * n 8 | for i in range(1, n): 9 | for j in range(0, i): 10 | if data[j] < data[i]: 11 | dp[i] = max(dp[i], dp[j] + 1) 12 | 13 | # 열외해야 하는 병사의 최소 수를 출력 14 | print(n - max(dp)) -------------------------------------------------------------------------------- /Exams/solutions/B_3.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | n, k = map(int, input().split()) 3 | 4 | # N x N 크기의 보드 전체를 0으로 초기화 5 | board = [] 6 | data = [] 7 | 8 | for i in range(n): 9 | # 보드 정보를 한 줄 단위로 입력 10 | board.append(list(map(int, input().split()))) 11 | for j in range(n): 12 | # 해당 위치에 바이러스가 존재하는 경우 13 | if board[i][j] != 0: 14 | # (바이러스 종류, 시간, 위치 X, 위치 Y) 삽입 15 | data.append((board[i][j], 0, i, j)) 16 | 17 | # 정렬 이후에 큐로 옮기기 18 | data.sort() 19 | q = deque(data) 20 | 21 | target_s, target_x, target_y = map(int, input().split()) 22 | 23 | # 바이러스가 퍼져나갈 수 있는 4가지의 위치 24 | dx = [-1, 0, 1, 0] 25 | dy = [0, 1, 0, -1] 26 | 27 | while q: 28 | virus, s, x, y = q.popleft() 29 | # 정확히 s초가 지나거나, 큐가 빌 때까지 반복 30 | if s == target_s: 31 | break 32 | # 4가지 위치를 각각 확인 33 | for i in range(4): 34 | nx = x + dx[i] 35 | ny = y + dy[i] 36 | # 해당 위치로 이동할 수 있는 경우 37 | if 0 <= nx and nx < n and 0 <= ny and ny < n: 38 | # 방문한 적 없다면, 그 위치에 바이러스 넣기 39 | if board[nx][ny] == 0: 40 | board[nx][ny] = virus 41 | q.append((virus, s + 1, nx, ny)) 42 | 43 | print(board[target_x - 1][target_y - 1]) -------------------------------------------------------------------------------- /Exams/solutions/C_1.py: -------------------------------------------------------------------------------- 1 | n, k = map(int, input().split()) 2 | count = 0 3 | 4 | # 0시 0분 0초부터 N시 59분 59초까지의 모든 시각 확인 5 | for h in range(n + 1): 6 | for m in range(60): 7 | for s in range(60): 8 | hour = str(h) if h >= 10 else '0' + str(h) 9 | minute = str(m) if m >= 10 else '0' + str(m) 10 | second = str(s) if s >= 10 else '0' + str(s) 11 | # K가 포함되는 경우 카운트 12 | if str(k) in hour + minute + second: 13 | count += 1 14 | 15 | print(count) -------------------------------------------------------------------------------- /Exams/solutions/C_2.py: -------------------------------------------------------------------------------- 1 | n, m, h = map(int, input().split()) 2 | a = [] 3 | d = [0] * (h + 1) 4 | for i in range(n): 5 | a.append(list(map(int, input().split()))) 6 | 7 | d[0] = 1 8 | 9 | # 모든 학생에 대하여 처리 10 | for i in range(n): 11 | data = [] 12 | # 0부터 h까지의 모든 높이에 대하여 처리 13 | for j in range(h + 1): 14 | for k in range(len(a[i])): 15 | # 현재 학생의 블록으로 특정 높이의 탑을 쌓을 수 있는 경우 16 | if d[j] != 0 and j + a[i][k] <= h: 17 | data.append((j + a[i][k], d[j])) 18 | # 쌓을 수 있는 높이에 대하여, 경우의 수 증가 19 | for height, value in data: 20 | d[height] = (d[height] + value) % 10007 21 | 22 | print(d[h]) -------------------------------------------------------------------------------- /Exams/solutions/C_3.py: -------------------------------------------------------------------------------- 1 | from itertools import combinations 2 | 3 | n = int(input()) 4 | a = [] 5 | teachers = [] 6 | spaces = [] 7 | for i in range(n): 8 | a.append(list(input().split())) 9 | for j in range(n): 10 | # 선생님이 존재하는 위치 저장 11 | if a[i][j] == 'T': 12 | teachers.append((i, j)) 13 | # 장애물을 설치할 수 있는 위치 저장 14 | if a[i][j] == 'X': 15 | spaces.append((i, j)) 16 | 17 | # 특정 방향으로 감시를 진행 (학생 발견: True, 학생 미발견: False) 18 | def watch(x, y, direction): 19 | # 왼쪽 방향으로 감시 20 | if direction == 0: 21 | while y >= 0: 22 | if a[x][y] == 'S': # 학생이 있는 경우 23 | return True 24 | if a[x][y] == 'O': # 장애물이 있는 경우 25 | return False 26 | y -= 1 27 | # 오른쪽 방향으로 감시 28 | if direction == 1: 29 | while y < n: 30 | if a[x][y] == 'S': # 학생이 있는 경우 31 | return True 32 | if a[x][y] == 'O': # 장애물이 있는 경우 33 | return False 34 | y += 1 35 | # 위쪽 방향으로 감시 36 | if direction == 2: 37 | while x >= 0: 38 | if a[x][y] == 'S': # 학생이 있는 경우 39 | return True 40 | if a[x][y] == 'O': # 장애물이 있는 경우 41 | return False 42 | x -= 1 43 | # 아래쪽 방향으로 감시 44 | if direction == 3: 45 | while x < n: 46 | if a[x][y] == 'S': # 학생이 있는 경우 47 | return True 48 | if a[x][y] == 'O': # 장애물이 있는 경우 49 | return False 50 | x += 1 51 | return False 52 | 53 | # 장애물 설치 이후에, 한 명이라도 학생이 감지되는지 검사 54 | def process(): 55 | # 모든 선생의 위치를 하나씩 확인 56 | for x, y in teachers: 57 | # 4가지 방향으로 학생을 감지할 수 있는지 확인 58 | for i in range(4): 59 | if watch(x, y, i): 60 | return True 61 | return False 62 | 63 | find = False 64 | for data in combinations(spaces, 3): 65 | for x, y in data: 66 | a[x][y] = 'O' 67 | # 학생이 한 명도 감지되지 않는 경우 68 | if not process(): 69 | # 원하는 경우를 발견한 것임 70 | find = True 71 | break 72 | for x, y in data: 73 | a[x][y] = 'X' 74 | 75 | if find: 76 | print('YES') 77 | else: 78 | print('NO') -------------------------------------------------------------------------------- /Exams/solutions/D_1.py: -------------------------------------------------------------------------------- 1 | n = input() 2 | length = len(n) 3 | sum = 0 4 | 5 | # 왼쪽 부분의 자릿수의 합 더하기 6 | for i in range(length // 2): 7 | sum += int(n[i]) 8 | 9 | # 오른쪽 부분의 자릿수의 합 빼기 10 | for i in range(length // 2, length): 11 | sum -= int(n[i]) 12 | 13 | if sum == 0: 14 | print("LUCKY") 15 | else: 16 | print("READY") -------------------------------------------------------------------------------- /Exams/solutions/D_2.py: -------------------------------------------------------------------------------- 1 | from itertools import permutations 2 | 3 | n, k = map(int, input().split()) 4 | a = list(map(int, input().split())) 5 | result = 0 6 | 7 | # 운동 키트를 이용하는 모든 경우를 계산 8 | for data in permutations(a): 9 | check = True 10 | weight = 500 11 | # 매일매일의 운동 키트를 확인 12 | for i in data: 13 | weight += i 14 | weight -= k 15 | # 중량이 500보다 낮아지는 날이 있는지 체크 16 | if weight < 500: 17 | check = False 18 | break 19 | # 매일매일 항상 중량 500 이상이라면, 카운트 20 | if check: 21 | result += 1 22 | 23 | print(result) -------------------------------------------------------------------------------- /Exams/solutions/D_3.py: -------------------------------------------------------------------------------- 1 | n, m = map(int, input().split()) 2 | 3 | board = [] 4 | for _ in range(n): 5 | board.append(list(map(int, input().split()))) 6 | v = [[False] * m for _ in range(n)] 7 | 8 | def dfs(x, y): 9 | # 아래 끝까지 내려 온 경우 0을 반환 10 | if x == n: 11 | return 0 12 | # 오른쪽 끝까지 이동한 경우 아래로 한 칸 내려가기 13 | if y == m: 14 | return dfs(x + 1, 0) 15 | result = 0 16 | # (x, y)를 기준으로 1번째 모양 17 | if x + 1 < n and y + 1 < m and v[x][y] + v[x][y + 1] + v[x + 1][y + 1] == 0: 18 | v[x][y] = v[x][y + 1] = v[x + 1][y + 1] = True 19 | result = max(result, dfs(x, y + 1) + (board[x][y] + 2 * board[x][y + 1] + board[x + 1][y + 1])) 20 | v[x][y] = v[x][y + 1] = v[x + 1][y + 1] = False 21 | # (x, y)를 기준으로 2번째 모양 22 | if x + 1 < n and y + 1 < m and v[x][y + 1] + v[x + 1][y] + v[x + 1][y + 1] == 0: 23 | v[x][y + 1] = v[x + 1][y] = v[x + 1][y + 1] = True 24 | result = max(result, dfs(x, y + 1) + (board[x][y + 1] + board[x + 1][y] + 2 * board[x + 1][y + 1])) 25 | v[x][y + 1] = v[x + 1][y] = v[x + 1][y + 1] = False 26 | # (x, y)를 기준으로 3번째 모양 27 | if x + 1 < n and y + 1 < m and v[x][y] + v[x + 1][y] + v[x + 1][y + 1] == 0: 28 | v[x][y] = v[x + 1][y] = v[x + 1][y + 1] = True 29 | result = max(result, dfs(x, y + 1) + (board[x][y] + 2 * board[x + 1][y] + board[x + 1][y + 1])) 30 | v[x][y] = v[x + 1][y] = v[x + 1][y + 1] = False 31 | # (x, y)를 기준으로 4번째 모양 32 | if x + 1 < n and y + 1 < m and v[x][y] + v[x][y + 1] + v[x + 1][y] == 0: 33 | v[x][y] = v[x][y + 1] = v[x + 1][y] = True 34 | result = max(result, dfs(x, y + 1) + (2 * board[x][y] + board[x][y + 1] + board[x + 1][y])) 35 | v[x][y] = v[x][y + 1] = v[x + 1][y] = False 36 | # 현재 위치를 기준으로 부메랑을 만들지 않는 경우 37 | result = max(result, dfs(x, y + 1)) 38 | return result 39 | 40 | print(dfs(0, 0)) -------------------------------------------------------------------------------- /Exams/solutions/E_1.py: -------------------------------------------------------------------------------- 1 | # 100 x 100 x 100보다 큰 수로, 최적의 해 보장 2 | MAX_VALUE = 10000000 3 | x, y, p1, p2 = map(int, input().split()) 4 | 5 | # 모든 경우의 수를 포함하는 10,000,000까지 반복 6 | while p1 != p2 and p1 < MAX_VALUE: 7 | if p1 < p2: 8 | p1 += x 9 | else: 10 | p2 += y 11 | 12 | # 공통적으로 지나는 지점이 있다면 13 | if p1 < MAX_VALUE: 14 | print(p1) 15 | # 공통적으로 지나는 지점이 없다면 16 | else: 17 | print(-1) -------------------------------------------------------------------------------- /Exams/solutions/E_2.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | 3 | # 도시의 개수, 도로의 개수, 거리 정보, 출발 도시 번호 4 | n, m, k, x = map(int, input().split()) 5 | adj = [[] for _ in range(n + 1)] 6 | 7 | # 모든 도로 정보 입력 받기 8 | for _ in range(m): 9 | a, b = map(int, input().split()) 10 | adj[a].append(b) 11 | 12 | # 모든 노드에 대한 최단 거리 초기화 13 | distance = [-1] * (n + 1) 14 | distance[x] = 0 # 출발 도시까지의 거리는 0으로 설정 15 | 16 | # BFS 수행 17 | q = deque([x]) 18 | while q: 19 | now = q.popleft() 20 | # 이동할 수 있는 모든 도시를 확인 21 | for next_node in adj[now]: 22 | # 아직 방문하지 않은 도시라면 23 | if distance[next_node] == -1: 24 | # 최단 거리 갱신 25 | distance[next_node] = distance[now] + 1 26 | q.append(next_node) 27 | 28 | # 최단 거리가 K인 모든 도시의 번호를 오름차순으로 출력 29 | check = False 30 | for i in range(1, n + 1): 31 | if distance[i] == k: 32 | print(i) 33 | check = True 34 | 35 | # 만약 최단 거리가 K인 도시가 없다면, -1 출력 36 | if check == False: 37 | print(-1) -------------------------------------------------------------------------------- /Exams/solutions/E_3.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | 3 | n, k = map(int, input().split()) 4 | a = list(map(int, input().split())) 5 | visited = set(a) 6 | 7 | # 모든 샘터의 왼쪽, 오른쪽에 대하여 확인 8 | q = deque() 9 | for i in a: 10 | # (거리, 위치)를 큐에 삽입 11 | q.append((1, i - 1)) 12 | q.append((1, i + 1)) 13 | 14 | count = 0 15 | result = 0 16 | 17 | # BFS 수행 (K채의 집이 배치될 때까지) 18 | while count < k: 19 | distance, now = q.popleft() 20 | # 해당 위치에 샘터 혹은 집이 존재한다면 무시 21 | if now in visited: 22 | continue 23 | visited.add(now) 24 | # 해당 위치에 집을 배치 25 | result += distance 26 | # 그 위치의 왼쪽, 오른쪽에 대하여 다시 확인 27 | q.append((distance + 1, now - 1)) 28 | q.append((distance + 1, now + 1)) 29 | count += 1 30 | 31 | print(result) -------------------------------------------------------------------------------- /Java369/1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class Main { 5 | 6 | public static int n, m; 7 | public static String[] switches = new String[500]; 8 | public static int[] cnt = new int[500]; 9 | 10 | public static void main(String[] args) { 11 | FastReader fr = new FastReader(); 12 | 13 | n = fr.nextInt(); 14 | m = fr.nextInt(); 15 | 16 | // 각 스위치에 연결된 전등 정보 입력받기 17 | for (int i = 0; i < n; i++) { 18 | switches[i] = fr.nextLine(); 19 | // 각 전등마다 자신에게 연결된 스위치의 개수 입력받기 20 | for (int j = 0; j < m; j++) { 21 | if (switches[i].charAt(j) == '1') cnt[j]++; 22 | } 23 | } 24 | 25 | boolean found = false; 26 | // 두 개의 스위치 선택 27 | for (int i = 0; i < n; i++) { 28 | for (int j = i + 1; j < n; j++) { 29 | // 두 스위치를 없애도 모든 전등을 켜는게 가능한지의 여부 30 | boolean check = true; 31 | // 각 전등을 하나씩 확인하며 32 | for (int k = 0; k < m; k++) { 33 | int target = 0; 34 | if (switches[i].charAt(k) == '1') target++; 35 | if (switches[j].charAt(k) == '1') target++; 36 | // 현재의 두 스위치가 없으면 안 되는 전등이라면 37 | if (cnt[k] <= target) { 38 | check = false; 39 | } 40 | } 41 | // 두 스위치가 없어도 모든 전등이 켜질 수 있다면 42 | if (check) { 43 | System.out.println("YES"); 44 | found = true; 45 | break; 46 | } 47 | } 48 | if (found) break; 49 | } 50 | 51 | if (!found) System.out.println("NO"); 52 | } 53 | 54 | public static class FastReader { 55 | BufferedReader br; 56 | StringTokenizer st; 57 | 58 | public FastReader() { 59 | br = new BufferedReader(new InputStreamReader(System.in)); 60 | } 61 | 62 | public FastReader(String s) throws FileNotFoundException { 63 | br = new BufferedReader(new FileReader(new File(s))); 64 | } 65 | 66 | String next() { 67 | while (st == null || !st.hasMoreElements()) { 68 | try { 69 | st = new StringTokenizer(br.readLine()); 70 | } catch (IOException e) { 71 | e.printStackTrace(); 72 | } 73 | } 74 | return st.nextToken(); 75 | } 76 | 77 | int nextInt() { 78 | return Integer.parseInt(next()); 79 | } 80 | 81 | long nextLong() { 82 | return Long.parseLong(next()); 83 | } 84 | 85 | double nextDouble() { 86 | return Double.parseDouble(next()); 87 | } 88 | 89 | String nextLine() { 90 | String str = ""; 91 | try { 92 | str = br.readLine(); 93 | } catch (IOException e) { 94 | e.printStackTrace(); 95 | } 96 | return str; 97 | } 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /Java369/2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class Main { 5 | 6 | public static int n, k; 7 | public static int[] arr = new int[100000]; 8 | 9 | public static void main(String[] args) { 10 | FastReader fr = new FastReader(); 11 | 12 | n = fr.nextInt(); 13 | k = fr.nextInt(); 14 | // 모든 공격력 입력받기 15 | for (int i = 0; i < n; i++) { 16 | arr[i] = fr.nextInt(); 17 | } 18 | // 앞에서부터 하나씩 공격력을 더해서 그 합이 k보다 커지는 지점 찾기 19 | long sum = 0; 20 | int maxValue = arr[0]; 21 | int maxIndex = 0; 22 | for (int i = 0; i < n; i++) { 23 | // 가장 큰 공격력의 값과 인덱스 기록 24 | if (maxValue < arr[i]) { 25 | maxValue = arr[i]; 26 | maxIndex = i; 27 | } 28 | // 합이 k보다 크다면 중지 29 | sum += arr[i]; 30 | if (sum > k) break; 31 | } 32 | // 건너뛰지 않아도 괜찮은 경우 33 | if (sum <= k) { 34 | System.out.println(n); 35 | } 36 | // 건너뛰어야 하는 경우 37 | else { 38 | // 가장 큰 공격력 제거 39 | arr[maxIndex] = 0; 40 | sum = 0; 41 | // 다시 앞에서부터 하나씩 공격력을 더해서 정답 계산 42 | for (int i = 0; i < n; i++) { 43 | sum += arr[i]; 44 | if (sum > k) { 45 | System.out.println(i - 1); 46 | break; 47 | } 48 | } 49 | if (sum <= k) { 50 | System.out.println(n - 1); 51 | } 52 | } 53 | } 54 | 55 | public static class FastReader { 56 | BufferedReader br; 57 | StringTokenizer st; 58 | 59 | public FastReader() { 60 | br = new BufferedReader(new InputStreamReader(System.in)); 61 | } 62 | 63 | public FastReader(String s) throws FileNotFoundException { 64 | br = new BufferedReader(new FileReader(new File(s))); 65 | } 66 | 67 | String next() { 68 | while (st == null || !st.hasMoreElements()) { 69 | try { 70 | st = new StringTokenizer(br.readLine()); 71 | } catch (IOException e) { 72 | e.printStackTrace(); 73 | } 74 | } 75 | return st.nextToken(); 76 | } 77 | 78 | int nextInt() { 79 | return Integer.parseInt(next()); 80 | } 81 | 82 | long nextLong() { 83 | return Long.parseLong(next()); 84 | } 85 | 86 | double nextDouble() { 87 | return Double.parseDouble(next()); 88 | } 89 | 90 | String nextLine() { 91 | String str = ""; 92 | try { 93 | str = br.readLine(); 94 | } catch (IOException e) { 95 | e.printStackTrace(); 96 | } 97 | return str; 98 | } 99 | } 100 | } -------------------------------------------------------------------------------- /Java369/3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class Main { 5 | 6 | public static int n; 7 | public static int[] arr, flag, result; 8 | 9 | public static void main(String[] args) { 10 | FastReader fr = new FastReader(); 11 | 12 | n = fr.nextInt(); 13 | arr = new int[n]; 14 | flag = new int[n]; 15 | 16 | // 각 원소의 값 입력 받기 17 | for (int i = 0; i < n; i++) { 18 | arr[i] = fr.nextInt(); 19 | } 20 | // 각 원소의 고정 여부 입력 받기 21 | for (int i = 0; i < n; i++) { 22 | flag[i] = fr.nextInt(); 23 | } 24 | 25 | // 고정된 원소를 제외한 다른 원소를 내림차순 정렬 26 | LinkedList list = new LinkedList(); 27 | for (int i = 0; i < n; i++) { 28 | if (flag[i] == 0) { 29 | list.add(arr[i]); 30 | } 31 | } 32 | Collections.sort(list, Collections.reverseOrder()); 33 | 34 | // 최종적인 배열 35 | result = new int[n]; 36 | for (int i = 0; i < n; i++) { 37 | if (flag[i] == 0) result[i] = list.removeFirst(); 38 | else result[i] = arr[i]; 39 | } 40 | 41 | // 결과 계산 42 | int k = 0; 43 | long sum = 0; 44 | for (int i = 0; i < n; i++) { 45 | sum += result[i]; 46 | if (sum < 0) { 47 | k = i + 1; 48 | } 49 | } 50 | System.out.println(k); 51 | } 52 | 53 | public static class FastReader { 54 | BufferedReader br; 55 | StringTokenizer st; 56 | 57 | public FastReader() { 58 | br = new BufferedReader(new InputStreamReader(System.in)); 59 | } 60 | 61 | public FastReader(String s) throws FileNotFoundException { 62 | br = new BufferedReader(new FileReader(new File(s))); 63 | } 64 | 65 | String next() { 66 | while (st == null || !st.hasMoreElements()) { 67 | try { 68 | st = new StringTokenizer(br.readLine()); 69 | } catch (IOException e) { 70 | e.printStackTrace(); 71 | } 72 | } 73 | return st.nextToken(); 74 | } 75 | 76 | int nextInt() { 77 | return Integer.parseInt(next()); 78 | } 79 | 80 | long nextLong() { 81 | return Long.parseLong(next()); 82 | } 83 | 84 | double nextDouble() { 85 | return Double.parseDouble(next()); 86 | } 87 | 88 | String nextLine() { 89 | String str = ""; 90 | try { 91 | str = br.readLine(); 92 | } catch (IOException e) { 93 | e.printStackTrace(); 94 | } 95 | return str; 96 | } 97 | } 98 | } -------------------------------------------------------------------------------- /Java369/4.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class Main { 5 | 6 | public static int na, nb; 7 | public static int[] a; 8 | public static int[] b; 9 | public static long result; 10 | 11 | public static int lowerBound(int[] arr, int target, int start, int end) { 12 | while (start < end) { 13 | int mid = (start + end) / 2; 14 | if (arr[mid] >= target) end = mid; 15 | else start = mid + 1; 16 | } 17 | return end; 18 | } 19 | 20 | public static int upperBound(int[] arr, int target, int start, int end) { 21 | while (start < end) { 22 | int mid = (start + end) / 2; 23 | if (arr[mid] > target) end = mid; 24 | else start = mid + 1; 25 | } 26 | return end; 27 | } 28 | 29 | public static void process(int[] a, int[] b) { 30 | for (int x = 0; x < a.length; x++) { 31 | int index = upperBound(b, a[x], 0, b.length) - 1; 32 | if (index == -1) continue; 33 | result = Math.min(result, 34 | (long) (a[x] - b[index]) * (a[x] - b[index]) 35 | ); 36 | } 37 | } 38 | 39 | public static void main(String[] args) { 40 | FastReader fr = new FastReader(); 41 | 42 | // 국가 A와 B에 대한 데이터 입력받기 43 | na = fr.nextInt(); 44 | nb = fr.nextInt(); 45 | a = new int[na]; 46 | b = new int[nb]; 47 | for (int i = 0; i < na; i++) { 48 | a[i] = fr.nextInt(); 49 | } 50 | for (int i = 0; i < nb; i++) { 51 | b[i] = fr.nextInt(); 52 | } 53 | 54 | // 정렬 수행 55 | Arrays.sort(a); 56 | Arrays.sort(b); 57 | 58 | // 정렬된 배열에 대하여 이진 탐색을 통해 정답 계산 59 | result = (long) 9e18; 60 | process(a, b); 61 | process(b, a); 62 | System.out.println(result); 63 | } 64 | 65 | public static class FastReader { 66 | BufferedReader br; 67 | StringTokenizer st; 68 | 69 | public FastReader() { 70 | br = new BufferedReader(new InputStreamReader(System.in)); 71 | } 72 | 73 | public FastReader(String s) throws FileNotFoundException { 74 | br = new BufferedReader(new FileReader(new File(s))); 75 | } 76 | 77 | String next() { 78 | while (st == null || !st.hasMoreElements()) { 79 | try { 80 | st = new StringTokenizer(br.readLine()); 81 | } catch (IOException e) { 82 | e.printStackTrace(); 83 | } 84 | } 85 | return st.nextToken(); 86 | } 87 | 88 | int nextInt() { 89 | return Integer.parseInt(next()); 90 | } 91 | 92 | long nextLong() { 93 | return Long.parseLong(next()); 94 | } 95 | 96 | double nextDouble() { 97 | return Double.parseDouble(next()); 98 | } 99 | 100 | String nextLine() { 101 | String str = ""; 102 | try { 103 | str = br.readLine(); 104 | } catch (IOException e) { 105 | e.printStackTrace(); 106 | } 107 | return str; 108 | } 109 | } 110 | } -------------------------------------------------------------------------------- /Java369/5.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class Main { 5 | 6 | public static class Pair { 7 | public int first; 8 | public int second; 9 | 10 | Pair(int first, int second) { 11 | this.first = first; 12 | this.second = second; 13 | } 14 | } 15 | 16 | public static Pair dfs(int x, int parent) { 17 | int g = 0; 18 | int b = 0; 19 | if (arr[x] == 1) g = 1; 20 | else if (arr[x] == 2) b = 1; 21 | for (int i = 0; i < graph.get(x).size(); i++) { 22 | int y = graph.get(x).get(i); 23 | if (y == parent) continue; // 자기 부모로 다시 가지 않도록 방지 24 | Pair temp = dfs(y, x); 25 | // 서브 트리에 초록색만 포함된 경우 26 | if (temp.first == green && temp.second == 0) { 27 | result++; 28 | } 29 | // 서브 트리에 파란색만 포함된 경우 30 | if (temp.first == 0 && temp.second == blue) { 31 | result++; 32 | } 33 | g += temp.first; 34 | b += temp.second; 35 | } 36 | return new Pair(g, b); 37 | } 38 | 39 | public static int n; 40 | public static int green, blue; 41 | public static int result; 42 | public static int[] arr = new int[500000]; 43 | public static ArrayList> graph = new ArrayList>(); 44 | 45 | public static void main(String[] args) { 46 | FastReader fr = new FastReader(); 47 | n = fr.nextInt(); 48 | for (int i = 0; i < n; i++) { 49 | arr[i] = fr.nextInt(); 50 | if (arr[i] == 1) green += 1; 51 | else if (arr[i] == 2) blue += 1; 52 | } 53 | // 그래프 초기화 54 | for (int i = 0; i < n; i++) { 55 | graph.add(new ArrayList()); 56 | } 57 | for (int i = 0; i < n - 1; i++) { 58 | int x = fr.nextInt() - 1; 59 | int y = fr.nextInt() - 1; 60 | graph.get(x).add(y); 61 | graph.get(y).add(x); 62 | } 63 | // 0번 노드를 기준으로 DFS 수행 64 | dfs(0, -1); 65 | System.out.println(result); 66 | } 67 | 68 | public static class FastReader { 69 | BufferedReader br; 70 | StringTokenizer st; 71 | 72 | public FastReader() { 73 | br = new BufferedReader(new InputStreamReader(System.in)); 74 | } 75 | 76 | public FastReader(String s) throws FileNotFoundException { 77 | br = new BufferedReader(new FileReader(new File(s))); 78 | } 79 | 80 | String next() { 81 | while (st == null || !st.hasMoreElements()) { 82 | try { 83 | st = new StringTokenizer(br.readLine()); 84 | } catch (IOException e) { 85 | e.printStackTrace(); 86 | } 87 | } 88 | return st.nextToken(); 89 | } 90 | 91 | int nextInt() { 92 | return Integer.parseInt(next()); 93 | } 94 | 95 | long nextLong() { 96 | return Long.parseLong(next()); 97 | } 98 | 99 | double nextDouble() { 100 | return Double.parseDouble(next()); 101 | } 102 | 103 | String nextLine() { 104 | String str = ""; 105 | try { 106 | str = br.readLine(); 107 | } catch (IOException e) { 108 | e.printStackTrace(); 109 | } 110 | return str; 111 | } 112 | } 113 | } -------------------------------------------------------------------------------- /Java369/6.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class Main { 5 | 6 | public static int n, r; 7 | public static int[] t = new int[100001]; 8 | public static int[] x = new int[100001]; 9 | public static int[] y = new int[100001]; 10 | public static int[] d = new int[100001]; 11 | public static int maxValue; 12 | 13 | public static void main(String[] args) { 14 | FastReader fr = new FastReader(); 15 | n = fr.nextInt(); 16 | r = fr.nextInt(); 17 | 18 | // 처음(0초)에는 항상 (1, 1)의 위치 19 | x[0] = 1; 20 | y[0] = 1; 21 | for (int i = 1; i <= n; i++) { 22 | t[i] = fr.nextInt(); 23 | x[i] = fr.nextInt(); 24 | y[i] = fr.nextInt(); 25 | d[i] = (int) -1e9; // 기본적으로 도달 불가능하다고 가정 26 | // (2 * r - 2) 범위인 경우에는 시간 내 도달 가능한지 확인 (오차 없이 정확함) 27 | for (int j = i - (2 * r - 2); j < i; j++) { 28 | if (j < 0) continue; 29 | // 시간 내 도달 가능하다면 30 | if (Math.abs(x[i] - x[j]) + Math.abs(y[i] - y[j]) <= t[i] - t[j]) { 31 | d[i] = Math.max(d[i], d[j] + 1); // 점화식에 따라 값 갱신 32 | } 33 | } 34 | // i가 커지면 무조건 [시간 내 도달 가능한] 경우가 생김 35 | if (i - 1 >= (2 * r - 2)) { 36 | // 바로 점화식에 따라서 갱신 37 | maxValue = Math.max(maxValue, d[i - 1 - (2 * r - 2)]); 38 | d[i] = Math.max(d[i], maxValue + 1); 39 | } 40 | } 41 | // 정답 출력 42 | int result = 0; 43 | for (int i = 0; i <= n; i++) { 44 | result = Math.max(result, d[i]); 45 | } 46 | System.out.println(result); 47 | } 48 | 49 | public static class FastReader { 50 | BufferedReader br; 51 | StringTokenizer st; 52 | 53 | public FastReader() { 54 | br = new BufferedReader(new InputStreamReader(System.in)); 55 | } 56 | 57 | public FastReader(String s) throws FileNotFoundException { 58 | br = new BufferedReader(new FileReader(new File(s))); 59 | } 60 | 61 | String next() { 62 | while (st == null || !st.hasMoreElements()) { 63 | try { 64 | st = new StringTokenizer(br.readLine()); 65 | } catch (IOException e) { 66 | e.printStackTrace(); 67 | } 68 | } 69 | return st.nextToken(); 70 | } 71 | 72 | int nextInt() { 73 | return Integer.parseInt(next()); 74 | } 75 | 76 | long nextLong() { 77 | return Long.parseLong(next()); 78 | } 79 | 80 | double nextDouble() { 81 | return Double.parseDouble(next()); 82 | } 83 | 84 | String nextLine() { 85 | String str = ""; 86 | try { 87 | str = br.readLine(); 88 | } catch (IOException e) { 89 | e.printStackTrace(); 90 | } 91 | return str; 92 | } 93 | } 94 | } -------------------------------------------------------------------------------- /Java369/7.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class Main { 5 | 6 | public static int n; 7 | public static int[] arr = new int[300001]; 8 | public static int[] leaves = new int[300001]; 9 | public static long[] sum = new long[300001]; 10 | public static ArrayList> graph = new ArrayList>(); 11 | public static long result; 12 | 13 | // 단방향 트리 구조이므로, 방문 정보를 기록할 필요 없음 14 | public static void dfs(int x) { 15 | // 리프 노드인 경우 16 | if (graph.get(x).size() == 0) { 17 | leaves[x] = 1; 18 | } 19 | // 인접 노드를 하나씩 확인하며 20 | for (int i = 0; i < graph.get(x).size(); i++) { 21 | int y = graph.get(x).get(i); 22 | dfs(y); 23 | // 서브 트리에 포함된 원소의 합과 리프 노드의 수 계산 24 | sum[x] += sum[y]; 25 | leaves[x] += leaves[y]; 26 | } 27 | result = Math.max(result, (long) Math.ceil((double) sum[x] / leaves[x])); 28 | } 29 | 30 | public static void main(String[] args) { 31 | FastReader fr = new FastReader(); 32 | n = fr.nextInt(); 33 | // 그래프 초기화 34 | for (int i = 0; i <= n; i++) { 35 | graph.add(new ArrayList()); 36 | } 37 | for (int i = 2; i <= n; i++) { 38 | int x = fr.nextInt(); 39 | graph.get(x).add(i); 40 | } 41 | // 각 노드의 값 입력받기 42 | for (int i = 1; i <= n; i++) { 43 | arr[i] = fr.nextInt(); 44 | sum[i] = arr[i]; 45 | } 46 | dfs(1); 47 | System.out.println(result); 48 | } 49 | 50 | public static class FastReader { 51 | BufferedReader br; 52 | StringTokenizer st; 53 | 54 | public FastReader() { 55 | br = new BufferedReader(new InputStreamReader(System.in)); 56 | } 57 | 58 | public FastReader(String s) throws FileNotFoundException { 59 | br = new BufferedReader(new FileReader(new File(s))); 60 | } 61 | 62 | String next() { 63 | while (st == null || !st.hasMoreElements()) { 64 | try { 65 | st = new StringTokenizer(br.readLine()); 66 | } catch (IOException e) { 67 | e.printStackTrace(); 68 | } 69 | } 70 | return st.nextToken(); 71 | } 72 | 73 | int nextInt() { 74 | return Integer.parseInt(next()); 75 | } 76 | 77 | long nextLong() { 78 | return Long.parseLong(next()); 79 | } 80 | 81 | double nextDouble() { 82 | return Double.parseDouble(next()); 83 | } 84 | 85 | String nextLine() { 86 | String str = ""; 87 | try { 88 | str = br.readLine(); 89 | } catch (IOException e) { 90 | e.printStackTrace(); 91 | } 92 | return str; 93 | } 94 | } 95 | } -------------------------------------------------------------------------------- /Java369/8.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class Main { 5 | 6 | public static class Pair { 7 | public int first; 8 | public int second; 9 | 10 | Pair(int first, int second) { 11 | this.first = first; 12 | this.second = second; 13 | } 14 | } 15 | 16 | public static int n, m; 17 | public static Pair[] mine = new Pair[3000]; 18 | public static Pair[] enemy = new Pair[3000]; 19 | public static int[] map = new int[1000002]; 20 | 21 | public static void main(String[] args) { 22 | FastReader fr = new FastReader(); 23 | n = fr.nextInt(); 24 | m = fr.nextInt(); 25 | 26 | for (int i = 0; i < n; i++) { 27 | int a = fr.nextInt(); 28 | int b = fr.nextInt(); 29 | mine[i] = new Pair(a, b); 30 | } 31 | 32 | for (int i = 0; i < m; i++) { 33 | int c = fr.nextInt(); 34 | int d = fr.nextInt(); 35 | enemy[i] = new Pair(c, d); 36 | } 37 | 38 | // 위쪽으로 이동해야 하는 거리 계산 39 | for (int i = 0; i < m; i++) { 40 | Pair x = enemy[i]; 41 | for (int j = 0; j < n; j++) { 42 | Pair y = mine[j]; 43 | if (x.first >= y.first) { 44 | map[x.first - y.first] = Math.max(map[x.first - y.first], x.second - y.second + 1); 45 | } 46 | } 47 | } 48 | 49 | // 값 재조정 50 | int maxValue = 0; 51 | for (int i = 1000001; i >= 0; i--) { 52 | maxValue = Math.max(maxValue, map[i]); 53 | map[i] = maxValue; 54 | } 55 | 56 | // 최종 결과 출력 57 | int result = 1000001; 58 | for (int i = 0; i <= 1000001; i++) { 59 | result = Math.min(result, i + map[i]); 60 | } 61 | System.out.println(result); 62 | } 63 | 64 | public static class FastReader { 65 | BufferedReader br; 66 | StringTokenizer st; 67 | 68 | public FastReader() { 69 | br = new BufferedReader(new InputStreamReader(System.in)); 70 | } 71 | 72 | public FastReader(String s) throws FileNotFoundException { 73 | br = new BufferedReader(new FileReader(new File(s))); 74 | } 75 | 76 | String next() { 77 | while (st == null || !st.hasMoreElements()) { 78 | try { 79 | st = new StringTokenizer(br.readLine()); 80 | } catch (IOException e) { 81 | e.printStackTrace(); 82 | } 83 | } 84 | return st.nextToken(); 85 | } 86 | 87 | int nextInt() { 88 | return Integer.parseInt(next()); 89 | } 90 | 91 | long nextLong() { 92 | return Long.parseLong(next()); 93 | } 94 | 95 | double nextDouble() { 96 | return Double.parseDouble(next()); 97 | } 98 | 99 | String nextLine() { 100 | String str = ""; 101 | try { 102 | str = br.readLine(); 103 | } catch (IOException e) { 104 | e.printStackTrace(); 105 | } 106 | return str; 107 | } 108 | } 109 | } -------------------------------------------------------------------------------- /Java369/README.md: -------------------------------------------------------------------------------- 1 | ### Java369 2 | -------------------------------------------------------------------------------- /Notes/[01] CHAPTER 01. 기본 자료구조 - 기초 문제풀이.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ndb796/Fast_Campus_Algorithm_Lecture_Notes/d5948b7e9897219bba1cdd2fc68e36bacc03051a/Notes/[01] CHAPTER 01. 기본 자료구조 - 기초 문제풀이.pdf -------------------------------------------------------------------------------- /Notes/[02] CHAPTER 01. 기본 자료구조 - 핵심 유형 문제풀이.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ndb796/Fast_Campus_Algorithm_Lecture_Notes/d5948b7e9897219bba1cdd2fc68e36bacc03051a/Notes/[02] CHAPTER 01. 기본 자료구조 - 핵심 유형 문제풀이.pdf -------------------------------------------------------------------------------- /Notes/[03] CHAPTER 02. 고급 자료구조 - 핵심 유형 문제풀이.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ndb796/Fast_Campus_Algorithm_Lecture_Notes/d5948b7e9897219bba1cdd2fc68e36bacc03051a/Notes/[03] CHAPTER 02. 고급 자료구조 - 핵심 유형 문제풀이.pdf -------------------------------------------------------------------------------- /Notes/[04] CHAPTER 03. 기본 정렬 알고리즘 - 기초 문제풀이.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ndb796/Fast_Campus_Algorithm_Lecture_Notes/d5948b7e9897219bba1cdd2fc68e36bacc03051a/Notes/[04] CHAPTER 03. 기본 정렬 알고리즘 - 기초 문제풀이.pdf -------------------------------------------------------------------------------- /Notes/[05] CHAPTER 03. 기본 정렬 알고리즘 - 핵심 유형 문제풀이.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ndb796/Fast_Campus_Algorithm_Lecture_Notes/d5948b7e9897219bba1cdd2fc68e36bacc03051a/Notes/[05] CHAPTER 03. 기본 정렬 알고리즘 - 핵심 유형 문제풀이.pdf -------------------------------------------------------------------------------- /Notes/[06] CHAPTER 04. 재귀 호출 - 핵심 유형 문제풀이.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ndb796/Fast_Campus_Algorithm_Lecture_Notes/d5948b7e9897219bba1cdd2fc68e36bacc03051a/Notes/[06] CHAPTER 04. 재귀 호출 - 핵심 유형 문제풀이.pdf -------------------------------------------------------------------------------- /Notes/[07] CHAPTER 05. 고급 정렬 알고리즘 - 핵심 유형 문제풀이.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ndb796/Fast_Campus_Algorithm_Lecture_Notes/d5948b7e9897219bba1cdd2fc68e36bacc03051a/Notes/[07] CHAPTER 05. 고급 정렬 알고리즘 - 핵심 유형 문제풀이.pdf -------------------------------------------------------------------------------- /Notes/[08] CHAPTER 06. 기본 탐색 알고리즘 - 기초 문제풀이.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ndb796/Fast_Campus_Algorithm_Lecture_Notes/d5948b7e9897219bba1cdd2fc68e36bacc03051a/Notes/[08] CHAPTER 06. 기본 탐색 알고리즘 - 기초 문제풀이.pdf -------------------------------------------------------------------------------- /Notes/[09] CHAPTER 06. 기본 탐색 알고리즘 - 핵심 유형 문제풀이.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ndb796/Fast_Campus_Algorithm_Lecture_Notes/d5948b7e9897219bba1cdd2fc68e36bacc03051a/Notes/[09] CHAPTER 06. 기본 탐색 알고리즘 - 핵심 유형 문제풀이.pdf -------------------------------------------------------------------------------- /Notes/[10] CHAPTER 07. 고급 탐색 알고리즘 - 기초 문제풀이.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ndb796/Fast_Campus_Algorithm_Lecture_Notes/d5948b7e9897219bba1cdd2fc68e36bacc03051a/Notes/[10] CHAPTER 07. 고급 탐색 알고리즘 - 기초 문제풀이.pdf -------------------------------------------------------------------------------- /Notes/[11] CHAPTER 07. 고급 탐색 알고리즘 - 핵심 유형 문제풀이.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ndb796/Fast_Campus_Algorithm_Lecture_Notes/d5948b7e9897219bba1cdd2fc68e36bacc03051a/Notes/[11] CHAPTER 07. 고급 탐색 알고리즘 - 핵심 유형 문제풀이.pdf -------------------------------------------------------------------------------- /Notes/[12] CHAPTER 08. 동적 프로그래밍 - 기초 문제풀이.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ndb796/Fast_Campus_Algorithm_Lecture_Notes/d5948b7e9897219bba1cdd2fc68e36bacc03051a/Notes/[12] CHAPTER 08. 동적 프로그래밍 - 기초 문제풀이.pdf -------------------------------------------------------------------------------- /Notes/[13] CHAPTER 08. 동적 프로그래밍 - 핵심 유형 문제풀이.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ndb796/Fast_Campus_Algorithm_Lecture_Notes/d5948b7e9897219bba1cdd2fc68e36bacc03051a/Notes/[13] CHAPTER 08. 동적 프로그래밍 - 핵심 유형 문제풀이.pdf -------------------------------------------------------------------------------- /Notes/[14] CHAPTER 09. 그래프 기본 탐색 알고리즘 - 기초 문제풀이.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ndb796/Fast_Campus_Algorithm_Lecture_Notes/d5948b7e9897219bba1cdd2fc68e36bacc03051a/Notes/[14] CHAPTER 09. 그래프 기본 탐색 알고리즘 - 기초 문제풀이.pdf -------------------------------------------------------------------------------- /Notes/[15] CHAPTER 09. 그래프 기본 탐색 알고리즘 - 핵심 유형 문제풀이.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ndb796/Fast_Campus_Algorithm_Lecture_Notes/d5948b7e9897219bba1cdd2fc68e36bacc03051a/Notes/[15] CHAPTER 09. 그래프 기본 탐색 알고리즘 - 핵심 유형 문제풀이.pdf -------------------------------------------------------------------------------- /Notes/[16] CHAPTER 10. 그래프 고급 탐색 알고리즘 - 핵심 유형 문제풀이.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ndb796/Fast_Campus_Algorithm_Lecture_Notes/d5948b7e9897219bba1cdd2fc68e36bacc03051a/Notes/[16] CHAPTER 10. 그래프 고급 탐색 알고리즘 - 핵심 유형 문제풀이.pdf -------------------------------------------------------------------------------- /Notes/[17] CHAPTER 11. 탐욕 알고리즘 - 기초 문제풀이.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ndb796/Fast_Campus_Algorithm_Lecture_Notes/d5948b7e9897219bba1cdd2fc68e36bacc03051a/Notes/[17] CHAPTER 11. 탐욕 알고리즘 - 기초 문제풀이.pdf -------------------------------------------------------------------------------- /Notes/[18] CHAPTER 11. 탐욕 알고리즘 - 핵심 유형 문제풀이.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ndb796/Fast_Campus_Algorithm_Lecture_Notes/d5948b7e9897219bba1cdd2fc68e36bacc03051a/Notes/[18] CHAPTER 11. 탐욕 알고리즘 - 핵심 유형 문제풀이.pdf -------------------------------------------------------------------------------- /Notes/[19] CHAPTER 12. 백트래킹 - 핵심 유형 문제풀이.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ndb796/Fast_Campus_Algorithm_Lecture_Notes/d5948b7e9897219bba1cdd2fc68e36bacc03051a/Notes/[19] CHAPTER 12. 백트래킹 - 핵심 유형 문제풀이.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Fast Campus Algorithm Lecture Notes 2 | 3 | * 패스트 캠퍼스(Fast Campus) 알고리즘 강의 수강생들을 위한 나동빈 강사의 문제풀이 소스코드 저장소입니다. 4 | * 본 강의는 Python 3을 활용한 강의이지만, 대부분의 문제들에 대하여 C++11, Java 소스코드를 추가적으로 제공합니다. 5 | * 하단의 전체 소스코드들은 백준 온라인 저지(BOJ)에 제출하여 정답 처리를 받은 소스코드들입니다. 6 | * 소스코드와 관련한 궁금한 점이나 오류 관련 문의는 [Issues](https://github.com/ndb796/Fast_Campus_Algorithm_Lecture_Notes/issues) 탭을 이용하여 남겨주세요. 7 | 8 | ## 강의 목차(Lecture Contents) 9 | 10 | ### Chapter 01. 기본 자료구조 11 | 12 | > 리스트, 큐, 스택을 활용한 문제풀이 13 | 14 | #### [01] [기초 문제풀이](/Notes/%5B01%5D%20CHAPTER%2001.%20기본%20자료구조%20-%20기초%20문제풀이.pdf) 15 | * [음계](https://www.acmicpc.net/problem/2920): ([Python 답안](/Solutions/%5B01%5D_1.py) / [C++ 답안](/Solutions/%5B01%5D_1.cpp) / [Java 답안](/Solutions/%5B01%5D_1.java)) 16 | * [블랙잭](https://www.acmicpc.net/problem/2798): ([Python 답안](/Solutions/%5B01%5D_2.py) / [C++ 답안](/Solutions/%5B01%5D_2.cpp) / [Java 답안](/Solutions/%5B01%5D_2.java)) 17 | 18 | #### [02] [핵심 유형 문제풀이](/Notes/%5B02%5D%20CHAPTER%2001.%20기본%20자료구조%20-%20핵심%20유형%20문제풀이.pdf) 19 | * [스택 수열](https://www.acmicpc.net/problem/1874): ([Python 답안](/Solutions/%5B02%5D_1.py) / [C++ 답안](/Solutions/%5B02%5D_1.cpp) / [Java 답안](/Solutions/%5B02%5D_1.java)) 20 | * [프린터 큐](https://www.acmicpc.net/problem/1966): ([Python 답안](/Solutions/%5B02%5D_2.py) / [C++ 답안](/Solutions/%5B02%5D_2.cpp) / [Java 답안](/Solutions/%5B02%5D_2.java)) 21 | * [키로거](https://www.acmicpc.net/problem/5397): ([Python 답안](/Solutions/%5B02%5D_3.py) / [C++ 답안](/Solutions/%5B02%5D_3.cpp) / [Java 답안](/Solutions/%5B02%5D_3.java)) 22 | 23 | ### Chapter 02. 고급 자료구조 24 | 25 | > 해시, 집합을 활용한 문제풀이 26 | 27 | #### [03] [핵심 유형 문제풀이](/Notes/%5B03%5D%20CHAPTER%2002.%20고급%20자료구조%20-%20핵심%20유형%20문제풀이.pdf) 28 | * [SHA-256](https://www.acmicpc.net/problem/10930): ([Python 답안](/Solutions/%5B03%5D_1.py) / [Java 답안](/Solutions/%5B03%5D_1.java)) 29 | * [수 찾기](https://www.acmicpc.net/problem/1920): ([Python 답안](/Solutions/%5B03%5D_2.py) / [C++ 답안](/Solutions/%5B03%5D_2.cpp) / [Java 답안](/Solutions/%5B03%5D_2.java)) 30 | * [친구 네트워크](https://www.acmicpc.net/problem/4195): ([Python 답안](/Solutions/%5B03%5D_3.py) / [C++ 답안](/Solutions/%5B03%5D_3.cpp)) 31 | 32 | ### Chapter 03. 기본 정렬 알고리즘 33 | 34 | > 정렬 라이브러리, 키(Key) 기반 정렬, 계수 정렬을 활용한 문제풀이 35 | 36 | #### [04] [기초 문제풀이](/Notes/%5B04%5D%20CHAPTER%2003.%20기본%20정렬%20알고리즘%20-%20기초%20문제풀이.pdf) 37 | * [수 정렬하기](https://www.acmicpc.net/problem/2750): ([Python 답안](/Solutions/%5B04%5D_1.py) / [C++ 답안](/Solutions/%5B04%5D_1.cpp) / [Java 답안](/Solutions/%5B04%5D_1.java)) 38 | * [소트인사이드](https://www.acmicpc.net/problem/1427): ([Python 답안](/Solutions/%5B04%5D_2.py) / [C++ 답안](/Solutions/%5B04%5D_2.cpp) / [Java 답안](/Solutions/%5B04%5D_2.java)) 39 | 40 | #### [05] [핵심 유형 문제풀이](/Notes/%5B05%5D%20CHAPTER%2003.%20기본%20정렬%20알고리즘%20-%20핵심%20유형%20문제풀이.pdf) 41 | * [나이순 정렬](https://www.acmicpc.net/problem/10814): ([Python 답안](/Solutions/%5B05%5D_1.py) / [C++ 답안](/Solutions/%5B05%5D_1.cpp) / [Java 답안](/Solutions/%5B05%5D_1.java)) 42 | * [좌표 정렬하기](https://www.acmicpc.net/problem/11650): ([Python 답안](/Solutions/%5B05%5D_2.py) / [C++ 답안](/Solutions/%5B05%5D_2.cpp) / [Java 답안](/Solutions/%5B05%5D_2.java)) 43 | * [수 정렬하기 3](https://www.acmicpc.net/problem/10989): ([Python 답안](/Solutions/%5B05%5D_3.py) / [C++ 답안](/Solutions/%5B05%5D_3.cpp) / [Java 답안](/Solutions/%5B05%5D_3.java)) 44 | 45 | ### Chapter 04. 재귀 호출 46 | 47 | > 기본적인 재귀 함수 구현을 활용한 문제풀이 48 | 49 | #### [06] [핵심 유형 문제풀이](/Notes/%5B06%5D%20CHAPTER%2004.%20재귀%20호출%20-%20핵심%20유형%20문제풀이.pdf) 50 | * [피보나치 수](https://www.acmicpc.net/problem/2747): ([Python 답안](/Solutions/%5B06%5D_1.py) / [C++ 답안](/Solutions/%5B06%5D_1.cpp) / [Java 답안](/Solutions/%5B06%5D_1.java)) 51 | * [Z](https://www.acmicpc.net/problem/1074): ([Python 답안](/Solutions/%5B06%5D_2.py) / [C++ 답안](/Solutions/%5B06%5D_2.cpp) / [Java 답안](/Solutions/%5B06%5D_2.java)) 52 | * [0 만들기](https://www.acmicpc.net/problem/7490): ([Python 답안](/Solutions/%5B06%5D_3.py)) 53 | 54 | ### Chapter 05. 고급 정렬 알고리즘 55 | 56 | > 병합 정렬을 활용한 문제풀이 57 | 58 | #### [07] [핵심 유형 문제풀이](/Notes/%5B07%5D%20CHAPTER%2005.%20고급%20정렬%20알고리즘%20-%20핵심%20유형%20문제풀이.pdf) 59 | * [수 정렬하기 2](https://www.acmicpc.net/problem/2751): ([Python 답안](/Solutions/%5B07%5D_1.py) / [C++ 답안](/Solutions/%5B07%5D_1.cpp) / [Java 답안](/Solutions/%5B07%5D_1.java)) 60 | * [K 번째 수](https://www.acmicpc.net/problem/11004): ([Python 답안](/Solutions/%5B07%5D_2.py) / [C++ 답안](/Solutions/%5B07%5D_2.cpp) / [Java 답안](/Solutions/%5B07%5D_2.java)) 61 | 62 | ### Chapter 06. 기본 탐색 알고리즘 63 | 64 | > 순차 탐색, 이진 탐색을 활용한 문제풀이 65 | 66 | #### [08] [기초 문제풀이](/Notes/%5B08%5D%20CHAPTER%2006.%20기본%20탐색%20알고리즘%20-%20기초%20문제풀이.pdf) 67 | * [문서 검색](https://www.acmicpc.net/problem/1543): ([Python 답안](/Solutions/%5B08%5D_1.py) / [C++ 답안](/Solutions/%5B08%5D_1.cpp) / [Java 답안](/Solutions/%5B08%5D_1.java)) 68 | * [새](https://www.acmicpc.net/problem/1568): ([Python 답안](/Solutions/%5B08%5D_2.py) / [C++ 답안](/Solutions/%5B08%5D_2.cpp) / [Java 답안](/Solutions/%5B08%5D_2.java)) 69 | * [베스트셀러](https://www.acmicpc.net/problem/1302): ([Python 답안](/Solutions/%5B08%5D_3.py) / [C++ 답안](/Solutions/%5B08%5D_3.cpp) / [Java 답안](/Solutions/%5B08%5D_3.java)) 70 | * [트로피 진열](https://www.acmicpc.net/problem/1668): ([Python 답안](/Solutions/%5B08%5D_4.py) / [C++ 답안](/Solutions/%5B08%5D_4.cpp) / [Java 답안](/Solutions/%5B08%5D_4.java)) 71 | * [성 지키기](https://www.acmicpc.net/problem/1236): ([Python 답안](/Solutions/%5B08%5D_5.py) / [C++ 답안](/Solutions/%5B08%5D_5.cpp) / [Java 답안](/Solutions/%5B08%5D_5.java)) 72 | 73 | #### [09] [핵심 유형 문제풀이](/Notes/%5B09%5D%20CHAPTER%2006.%20기본%20탐색%20알고리즘%20-%20핵심%20유형%20문제풀이.pdf) 74 | * [공유기 설치](https://www.acmicpc.net/problem/2110): ([Python 답안](/Solutions/%5B09%5D_1.py) / [C++ 답안](/Solutions/%5B09%5D_1.cpp) / [Java 답안](/Solutions/%5B09%5D_1.java)) 75 | * [중량제한](https://www.acmicpc.net/problem/1939): ([Python 답안](/Solutions/%5B09%5D_2.py)) 76 | 77 | ### Chapter 07. 고급 탐색 알고리즘 78 | 79 | > 트리 순회, 힙 (우선순위 큐), 위상 정렬을 활용한 문제풀이 80 | 81 | #### [10] [기초 문제풀이](/Notes/%5B10%5D%20CHAPTER%2007.%20고급%20탐색%20알고리즘%20-%20기초%20문제풀이.pdf) 82 | * [트리 순회](https://www.acmicpc.net/problem/1991): ([Python 답안](/Solutions/%5B10%5D_1.py)) 83 | * [트리의 높이와 너비](https://www.acmicpc.net/problem/2250): ([Python 답안](/Solutions/%5B10%5D_2.py)) 84 | 85 | #### [11] [핵심 유형 문제풀이](/Notes/%5B11%5D%20CHAPTER%2007.%20고급%20탐색%20알고리즘%20-%20핵심%20유형%20문제풀이.pdf) 86 | * [최소 힙](https://www.acmicpc.net/problem/1927): ([Python 답안](/Solutions/%5B11%5D_1.py) / [C++ 답안](/Solutions/%5B11%5D_1.cpp) / [Java 답안](/Solutions/%5B11%5D_1.java)) 87 | * [카드 정렬하기](https://www.acmicpc.net/problem/1715): ([Python 답안](/Solutions/%5B11%5D_2.py) / [C++ 답안](/Solutions/%5B11%5D_2.cpp) / [Java 답안](/Solutions/%5B11%5D_2.java)) 88 | * [문제집](https://www.acmicpc.net/problem/1766): ([Python 답안](/Solutions/%5B11%5D_3.py)) 89 | 90 | ### Chapter 08. 동적 프로그래밍 91 | 92 | > 잘 알려진 동적 프로그래밍 개념 (타일링, Knapsack, LCS, LIS 등)을 활용한 문제풀이 93 | 94 | #### [12] [기초 문제풀이](/Notes/%5B12%5D%20CHAPTER%2008.%20동적%20프로그래밍%20-%20기초%20문제풀이.pdf) 95 | * [01타일](https://www.acmicpc.net/problem/1904): ([Python 답안](/Solutions/%5B12%5D_1.py) / [C++ 답안](/Solutions/%5B12%5D_1.cpp) / [Java 답안](/Solutions/%5B12%5D_1.java)) 96 | * [평범한 배낭](https://www.acmicpc.net/problem/12865): ([Python 답안](/Solutions/%5B12%5D_2.py) / [C++ 답안](/Solutions/%5B12%5D_2.cpp) / [Java 답안](/Solutions/%5B12%5D_2.java)) 97 | * [가장 긴 증가하는 부분 수열](https://www.acmicpc.net/problem/11053): ([Python 답안](/Solutions/%5B12%5D_3.py) / [C++ 답안](/Solutions/%5B12%5D_3.cpp) / [Java 답안](/Solutions/%5B12%5D_3.java)) 98 | 99 | #### [13] [핵심 유형 문제풀이](/Notes/%5B13%5D%20CHAPTER%2008.%20동적%20프로그래밍%20-%20핵심%20유형%20문제풀이.pdf) 100 | * [LCS](https://www.acmicpc.net/problem/9251): ([Python 답안](/Solutions/%5B13%5D_1.py) / [C++ 답안](/Solutions/%5B13%5D_1.cpp) / [Java 답안](/Solutions/%5B13%5D_1.java)) 101 | * [기타리스트](https://www.acmicpc.net/problem/1495): ([Python 답안](/Solutions/%5B13%5D_2.py) / [C++ 답안](/Solutions/%5B13%5D_2.cpp) / [Java 답안](/Solutions/%5B13%5D_2.java)) 102 | * [가장 높은 탑 쌓기](https://www.acmicpc.net/problem/2655): ([Python 답안](/Solutions/%5B13%5D_3.py)) 103 | 104 | ### Chapter 09. 그래프 기본 탐색 알고리즘 105 | 106 | > DFS와 BFS를 활용한 문제풀이 107 | 108 | #### [14] [기초 문제풀이](/Notes/%5B14%5D%20CHAPTER%2009.%20그래프%20기본%20탐색%20알고리즘%20-%20기초%20문제풀이.pdf) 109 | * [DFS와 BFS](https://www.acmicpc.net/problem/1260): ([Python 답안](/Solutions/%5B14%5D_1.py) / [C++ 답안](/Solutions/%5B14%5D_1.cpp) / [Java 답안](/Solutions/%5B14%5D_1.java)) 110 | * [숨바꼭질](https://www.acmicpc.net/problem/1697): ([Python 답안](/Solutions/%5B14%5D_2.py) / [C++ 답안](/Solutions/%5B14%5D_2.cpp) / [Java 답안](/Solutions/%5B14%5D_2.java)) 111 | 112 | #### [15] [핵심 유형 문제풀이](/Notes/%5B15%5D%20CHAPTER%2009.%20그래프%20기본%20탐색%20알고리즘%20-%20핵심%20유형%20문제풀이.pdf) 113 | * [바이러스](https://www.acmicpc.net/problem/2606): ([Python 답안](/Solutions/%5B15%5D_1.py) / [C++ 답안](/Solutions/%5B15%5D_1.cpp) / [Java 답안](/Solutions/%5B15%5D_1.java)) 114 | * [유기농 배추](https://www.acmicpc.net/problem/1012): ([Python 답안](/Solutions/%5B15%5D_2.py) / [C++ 답안](/Solutions/%5B15%5D_2.cpp) / [Java 답안](/Solutions/%5B15%5D_2.java)) 115 | * [효율적인 해킹](https://www.acmicpc.net/problem/1325): ([Python 답안](/Solutions/%5B15%5D_3.py) / [C++ 답안](/Solutions/%5B15%5D_3.cpp) / [Java 답안](/Solutions/%5B15%5D_3.java)) 116 | 117 | ### Chapter 10. 그래프 고급 탐색 알고리즘 118 | 119 | > 다익스트라 최단 경로 알고리즘, 최소 신장 트리 알고리즘을 활용한 문제풀이 120 | 121 | #### [16] [핵심 유형 문제풀이](/Notes/%5B16%5D%20CHAPTER%2010.%20그래프%20고급%20탐색%20알고리즘%20-%20핵심%20유형%20문제풀이.pdf) 122 | * [해킹](https://www.acmicpc.net/problem/10282): ([Python 답안](/Solutions/%5B16%5D_1.py) / [C++ 답안](/Solutions/%5B16%5D_1.cpp)) 123 | * [거의 최단 경로](https://www.acmicpc.net/problem/5719): ([Python 답안](/Solutions/%5B16%5D_2.py) / [C++ 답안](/Solutions/%5B16%5D_2.cpp)) 124 | * [우주신과의 교감](https://www.acmicpc.net/problem/1774): ([Python 답안](/Solutions/%5B16%5D_3.py) / [C++ 답안](/Solutions/%5B16%5D_3.cpp)) 125 | 126 | ### Chapter 11. 탐욕 알고리즘 127 | 128 | > 그리디(Greedy) 알고리즘의 원리를 활용한 문제풀이 129 | 130 | #### [17] [기초 문제풀이](/Notes/%5B17%5D%20CHAPTER%2011.%20탐욕%20알고리즘%20-%20기초%20문제풀이.pdf) 131 | * [거스름돈](https://www.acmicpc.net/problem/5585): ([Python 답안](/Solutions/%5B17%5D_1.py) / [C++ 답안](/Solutions/%5B17%5D_1.cpp) / [Java 답안](/Solutions/%5B17%5D_1.java)) 132 | * [뒤집기](https://www.acmicpc.net/problem/1439): ([Python 답안](/Solutions/%5B17%5D_2.py) / [C++ 답안](/Solutions/%5B17%5D_2.cpp) / [Java 답안](/Solutions/%5B17%5D_2.java)) 133 | * [등수 매기기](https://www.acmicpc.net/problem/2012): ([Python 답안](/Solutions/%5B17%5D_3.py) / [C++ 답안](/Solutions/%5B17%5D_3.cpp) / [Java 답안](/Solutions/%5B17%5D_3.java)) 134 | * [배](https://www.acmicpc.net/problem/1092): ([Python 답안](/Solutions/%5B17%5D_4.py) / [C++ 답안](/Solutions/%5B17%5D_4.cpp) / [Java 답안](/Solutions/%5B17%5D_4.java)) 135 | 136 | #### [18] [핵심 유형 문제풀이](/Notes/%5B18%5D%20CHAPTER%2011.%20탐욕%20알고리즘%20-%20핵심%20유형%20문제풀이.pdf) 137 | * [센서](https://www.acmicpc.net/problem/2212): ([Python 답안](/Solutions/%5B18%5D_1.py) / [C++ 답안](/Solutions/%5B18%5D_1.cpp) / [Java 답안](/Solutions/%5B18%5D_1.java)) 138 | * [도서관](https://www.acmicpc.net/problem/1461): ([Python 답안](/Solutions/%5B18%5D_2.py) / [C++ 답안](/Solutions/%5B18%5D_2.cpp) / [Java 답안](/Solutions/%5B18%5D_2.java)) 139 | * [컵라면](https://www.acmicpc.net/problem/1781): ([Python 답안](/Solutions/%5B18%5D_3.py) / [C++ 답안](/Solutions/%5B18%5D_3.cpp) / [Java 답안](/Solutions/%5B18%5D_3.java)) 140 | 141 | ### Chapter 12. 백 트래킹 142 | 143 | > DFS 혹은 조합(Combinations)을 활용한 문제풀이 144 | 145 | #### [19] [핵심 유형 문제풀이](/Notes/%5B19%5D%20CHAPTER%2012.%20백트래킹%20-%20핵심%20유형%20문제풀이.pdf) 146 | * [N-Queen](https://www.acmicpc.net/problem/9663): ([Python 답안](/Solutions/%5B19%5D_1.py) / [C++ 답안](/Solutions/%5B19%5D_1.cpp)) 147 | * [알파벳](https://www.acmicpc.net/problem/1987): ([Python 답안](/Solutions/%5B19%5D_2.py)) 148 | * [암호 만들기](https://www.acmicpc.net/problem/1759): ([Python 답안](/Solutions/%5B19%5D_3.py)) 149 | 150 | ## 코딩 테스트 실전 모의고사 문제집 (Mock Exams) 151 | 152 | * 본 문제집은 최신 코딩 테스트 출제 경향을 고려해 나동빈 강사가 자체 제작 및 번역하여 구성한 문제집입니다. 153 | * 본 문제집은 인터넷 검색이 불가능한 상황에서 제한 시간 안에 Python 3.7 언어를 이용하여 풀어주세요. 154 | * 모든 문제들은 검수 과정을 거쳐 백준 온라인 저지(BOJ)에 업로드 되어있습니다. 155 | 156 | ### 코딩 테스트 모의고사 A형 157 | 158 | * [A형 문제지](/Exams/coding_test_A.pdf) 159 | 160 | 난이도|문제 풀이시간|문제 개수|예상 합격 커트라인| 161 | |------|---|---|---| 162 | |중하|3시간|3문제|3문제| 163 | 164 | 1. [안테나](https://www.acmicpc.net/problem/18310): ([Python 답안](/Exams/solutions/A_1.py)) 165 | 2. [큰 수 구성하기](https://www.acmicpc.net/problem/18511): ([Python 답안](/Exams/solutions/A_2.py)) 166 | 3. [현명한 나이트](https://www.acmicpc.net/problem/18404): ([Python 답안](/Exams/solutions/A_3.py)) 167 | 168 | ### 코딩 테스트 모의고사 B형 169 | 170 | * [B형 문제지](/Exams/coding_test_B.pdf) 171 | 172 | 난이도|문제 풀이시간|문제 개수|예상 합격 커트라인| 173 | |------|---|---|---| 174 | |중|3시간|3문제|2~3문제| 175 | 176 | 1. [왕복](https://www.acmicpc.net/problem/18311): ([Python 답안](/Exams/solutions/B_1.py)) 177 | 2. [병사 배치하기](https://www.acmicpc.net/problem/18353): ([Python 답안](/Exams/solutions/B_2.py)) 178 | 3. [경쟁적 전염](https://www.acmicpc.net/problem/18405): ([Python 답안](/Exams/solutions/B_3.py)) 179 | 180 | ### 코딩 테스트 모의고사 C형 181 | 182 | * [C형 문제지](/Exams/coding_test_C.pdf) 183 | 184 | 난이도|문제 풀이시간|문제 개수|예상 합격 커트라인| 185 | |------|---|---|---| 186 | |상|3시간|3문제|2문제| 187 | 188 | 1. [시각](https://www.acmicpc.net/problem/18312): ([Python 답안](/Exams/solutions/C_1.py)) 189 | 2. [함께 블록 쌓기](https://www.acmicpc.net/problem/18427): ([Python 답안](/Exams/solutions/C_2.py)) 190 | 3. [감시 피하기](https://www.acmicpc.net/problem/18428): ([Python 답안](/Exams/solutions/C_3.py)) 191 | 192 | ### 코딩 테스트 모의고사 D형 193 | 194 | * [D형 문제지](/Exams/coding_test_D.pdf) 195 | 196 | 난이도|문제 풀이시간|문제 개수|예상 합격 커트라인| 197 | |------|---|---|---| 198 | |중상|3시간|3문제|2~3문제| 199 | 200 | 1. [럭키 스트레이트](https://www.acmicpc.net/problem/18406): ([Python 답안](/Exams/solutions/D_1.py)) 201 | 2. [근손실](https://www.acmicpc.net/problem/18429): ([Python 답안](/Exams/solutions/D_2.py)) 202 | 3. [무기 공학](https://www.acmicpc.net/problem/18430): ([Python 답안](/Exams/solutions/D_3.py)) 203 | 204 | ### 코딩 테스트 모의고사 E형 205 | 206 | * [E형 문제지](/Exams/coding_test_E.pdf) 207 | 208 | 난이도|문제 풀이시간|문제 개수|예상 합격 커트라인| 209 | |------|---|---|---| 210 | |중|3시간|3문제|2~3문제| 211 | 212 | 1. [점프 점프](https://www.acmicpc.net/problem/18512): ([Python 답안](/Exams/solutions/E_1.py)) 213 | 2. [특정 거리의 도시 찾기](https://www.acmicpc.net/problem/18352): ([Python 답안](/Exams/solutions/E_2.py)) 214 | 3. [샘터](https://www.acmicpc.net/problem/18513): ([Python 답안](/Exams/solutions/E_3.py)) 215 | -------------------------------------------------------------------------------- /Solutions/[01]_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int data[8]; 6 | bool ascending = true; 7 | bool descending = true; 8 | 9 | int main() { 10 | for (int i = 0; i < 8; i++) { 11 | cin >> data[i]; 12 | } 13 | 14 | for (int i = 1; i < 8; i++) { 15 | if (data[i] > data[i - 1]) descending = false; 16 | if (data[i] < data[i - 1]) ascending = false; 17 | } 18 | 19 | if (ascending) cout << "ascending"; 20 | else if (descending) cout << "descending"; 21 | else cout << "mixed"; 22 | } 23 | -------------------------------------------------------------------------------- /Solutions/[01]_1.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | Scanner sc = new Scanner(System.in); 7 | 8 | int[] data = new int[8]; 9 | boolean ascending = true; 10 | boolean descending = true; 11 | 12 | for (int i = 0; i < 8; i++) { 13 | data[i] = sc.nextInt(); 14 | } 15 | 16 | for (int i = 1; i < 8; i++) { 17 | if (data[i] > data[i - 1]) descending = false; 18 | if (data[i] < data[i - 1]) ascending = false; 19 | } 20 | 21 | if (ascending) System.out.println("ascending"); 22 | else if (descending) System.out.println("descending"); 23 | else System.out.println("mixed"); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Solutions/[01]_1.py: -------------------------------------------------------------------------------- 1 | a = list(map(int, input().split(' '))) 2 | 3 | ascending = True 4 | descending = True 5 | 6 | for i in range(1, 8): 7 | if a[i] > a[i - 1]: 8 | descending = False 9 | elif a[i] < a[i - 1]: 10 | ascending = False 11 | 12 | if ascending: 13 | print('ascending') 14 | elif descending: 15 | print('descending') 16 | else: 17 | print('mixed') -------------------------------------------------------------------------------- /Solutions/[01]_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int n, m; 6 | int data[100]; 7 | int result = 0; 8 | 9 | int main() { 10 | cin >> n >> m; 11 | for (int i = 0; i < n; i++) { 12 | cin >> data[i]; 13 | } 14 | for (int i = 0; i < n; i++) { 15 | for (int j = i + 1; j < n; j++) { 16 | for (int k = j + 1; k < n; k++) { 17 | int sum = data[i] + data[j] + data[k]; 18 | if (sum <= m) result = max(result, sum); 19 | } 20 | } 21 | } 22 | cout << result; 23 | } 24 | -------------------------------------------------------------------------------- /Solutions/[01]_2.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | Scanner sc = new Scanner(System.in); 7 | 8 | int n, m; 9 | int[] data = new int[100]; 10 | int result = 0; 11 | 12 | n = sc.nextInt(); 13 | m = sc.nextInt(); 14 | for (int i = 0; i < n; i++) { 15 | data[i] = sc.nextInt(); 16 | } 17 | 18 | for (int i = 0; i < n; i++) { 19 | for (int j = i + 1; j < n; j++) { 20 | for (int k = j + 1; k < n; k++) { 21 | int sum = data[i] + data[j] + data[k]; 22 | if (sum <= m) result = Math.max(result, sum); 23 | } 24 | } 25 | } 26 | System.out.println(result); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Solutions/[01]_2.py: -------------------------------------------------------------------------------- 1 | n, m = list(map(int, input().split(' '))) 2 | data = list(map(int, input().split(' '))) 3 | 4 | result = 0 5 | length = len(data) 6 | count = 0 7 | 8 | for i in range(0, length): 9 | for j in range(i + 1, length): 10 | for k in range(j + 1, length): 11 | sum_value = data[i] + data[j] + data[k] 12 | if sum_value <= m: 13 | result = max(result, sum_value) 14 | 15 | print(result) -------------------------------------------------------------------------------- /Solutions/[02]_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int n, cnt = 1; 6 | stack s; 7 | vector result; 8 | 9 | int main() { 10 | cin >> n; 11 | for (int i = 0; i < n; i++) { 12 | int x; 13 | cin >> x; 14 | // 입력 받은 데이터에 도달할 때까지 삽입 15 | while (cnt <= x) { 16 | s.push(cnt); 17 | cnt += 1; 18 | result.push_back('+'); 19 | } 20 | // 스택의 최상의 원소가 데이터와 같을 때 출력 21 | if (s.top() == x) { 22 | s.pop(); 23 | result.push_back('-'); 24 | } 25 | else { 26 | cout << "NO"; 27 | return 0; 28 | } 29 | } 30 | // 결과를 차례대로 출력 31 | for (auto x: result) { 32 | cout << x << '\n'; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Solutions/[02]_1.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Scanner; 3 | import java.util.Stack; 4 | 5 | public class Main { 6 | 7 | public static void main(String[] args) { 8 | Scanner sc = new Scanner(System.in); 9 | 10 | int n, cnt = 1; 11 | Stack s = new Stack(); 12 | ArrayList result = new ArrayList(); 13 | 14 | n = sc.nextInt(); 15 | for (int i = 0; i < n; i++) { 16 | int x = sc.nextInt(); 17 | // 입력 받은 데이터에 도달할 때까지 삽입 18 | while (cnt <= x) { 19 | s.push(cnt); 20 | cnt += 1; 21 | result.add('+'); 22 | } 23 | // 스택의 최상의 원소가 데이터와 같을 때 출력 24 | if (s.peek() == x) { 25 | s.pop(); 26 | result.add('-'); 27 | } 28 | else { 29 | System.out.println("NO"); 30 | return; 31 | } 32 | } 33 | 34 | // 결과를 차례대로 출력 35 | for (int i = 0; i < result.size(); i++) { 36 | System.out.println(result.get(i)); 37 | } 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Solutions/[02]_1.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ndb796/Fast_Campus_Algorithm_Lecture_Notes/d5948b7e9897219bba1cdd2fc68e36bacc03051a/Solutions/[02]_1.py -------------------------------------------------------------------------------- /Solutions/[02]_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int tc, n, m, cnt; 6 | 7 | int main() { 8 | cin >> tc; 9 | 10 | for (int i = 0; i < tc; i++) { 11 | queue> q; 12 | priority_queue pq; // 높은 중요도부터 처리 13 | cnt = 0; 14 | cin >> n >> m; 15 | 16 | for (int j = 0; j < n; j++) { 17 | int x; 18 | cin >> x; 19 | q.push({x, j}); // {중요도, 인덱스} 삽입 20 | pq.push(x); // 중요도 삽입 21 | } 22 | 23 | while (true) { 24 | int x = q.front().first; 25 | int index = q.front().second; 26 | q.pop(); 27 | 28 | // 가장 중요도가 높은 경우 출력 29 | if (x == pq.top()) { 30 | cnt++; 31 | pq.pop(); 32 | if (index == m) { 33 | cout << cnt << '\n'; 34 | break; 35 | } 36 | } 37 | else { 38 | q.push({x, index}); 39 | } 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Solutions/[02]_2.java: -------------------------------------------------------------------------------- 1 | import java.util.LinkedList; 2 | import java.util.PriorityQueue; 3 | import java.util.Queue; 4 | import java.util.Scanner; 5 | 6 | class Node { 7 | public int priority; 8 | public int index; 9 | 10 | public Node(int priority, int index) { 11 | this.priority = priority; 12 | this.index = index; 13 | } 14 | } 15 | 16 | public class Main { 17 | 18 | public static void main(String[] args) { 19 | Scanner sc = new Scanner(System.in); 20 | int tc, n, m, cnt; 21 | tc = sc.nextInt(); 22 | 23 | for (int i = 0; i < tc; i++) { 24 | Queue q = new LinkedList(); 25 | PriorityQueue pq = new PriorityQueue(); // 높은 중요도부터 처리 26 | cnt = 0; 27 | n = sc.nextInt(); 28 | m = sc.nextInt(); 29 | 30 | for (int j = 0; j < n; j++) { 31 | int x = sc.nextInt(); 32 | q.add(new Node(x, j)); // {중요도, 인덱스} 삽입 33 | pq.add(-x); // 중요도 삽입 (내림차순) 34 | } 35 | 36 | while (true) { 37 | Node node = q.poll(); 38 | 39 | // 가장 중요도가 높은 경우 출력 40 | if (node.priority == -pq.peek()) { 41 | cnt++; 42 | pq.poll(); 43 | if (node.index == m) { 44 | System.out.println(cnt); 45 | break; 46 | } 47 | } 48 | else { 49 | q.add(new Node(node.priority, node.index)); 50 | } 51 | } 52 | } 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /Solutions/[02]_2.py: -------------------------------------------------------------------------------- 1 | test_case = int(input()) 2 | 3 | for _ in range(test_case): 4 | n, m = list(map(int, input().split(' '))) 5 | queue = list(map(int, input().split(' '))) 6 | queue = [(i, idx) for idx, i in enumerate(queue)] 7 | count = 0 8 | 9 | while True: 10 | if queue[0][0] == max(queue, key=lambda x: x[0])[0]: 11 | count += 1 12 | if queue[0][1] == m: 13 | print(count) 14 | break 15 | else: 16 | queue.pop(0) 17 | else: 18 | queue.append(queue.pop(0)) -------------------------------------------------------------------------------- /Solutions/[02]_3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int tc; 6 | 7 | int main() { 8 | cin >> tc; 9 | 10 | while (tc--) { 11 | string s; 12 | cin >> s; 13 | 14 | stack left; 15 | stack right; 16 | 17 | for (int i = 0; i < s.length(); i++) { 18 | if (s[i] == '-') { 19 | if (!left.empty()) left.pop(); 20 | } 21 | else if (s[i] == '<') { 22 | if (!left.empty()) { 23 | right.push(left.top()); 24 | left.pop(); 25 | } 26 | } 27 | else if (s[i] == '>') { 28 | if (!right.empty()) { 29 | left.push(right.top()); 30 | right.pop(); 31 | } 32 | } 33 | else { 34 | left.push(s[i]); 35 | } 36 | } 37 | 38 | vector result; 39 | while (!right.empty()) { 40 | left.push(right.top()); 41 | right.pop(); 42 | } 43 | while (!left.empty()) { 44 | result.push_back(left.top()); 45 | left.pop(); 46 | } 47 | reverse(result.begin(), result.end()); 48 | for (auto x: result) { 49 | cout << x; 50 | } 51 | cout << '\n'; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Solutions/[02]_3.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.util.Stack; 5 | 6 | public class Main { 7 | 8 | public static void main(String[] args) throws IOException { 9 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 10 | int tc; 11 | tc = Integer.parseInt(br.readLine()); 12 | 13 | while (tc-- > 0) { 14 | String s; 15 | s = br.readLine(); 16 | 17 | Stack left = new Stack(); 18 | Stack right = new Stack(); 19 | 20 | for (int i = 0; i < s.length(); i++) { 21 | if (s.charAt(i) == '-') { 22 | if (!left.empty()) left.pop(); 23 | } 24 | else if (s.charAt(i) == '<') { 25 | if (!left.empty()) { 26 | right.push(left.pop()); 27 | } 28 | } 29 | else if (s.charAt(i) == '>') { 30 | if (!right.empty()) { 31 | left.push(right.pop()); 32 | } 33 | } 34 | else { 35 | left.push(s.charAt(i)); 36 | } 37 | } 38 | 39 | StringBuilder result = new StringBuilder(); 40 | while (!right.empty()) { 41 | left.push(right.pop()); 42 | } 43 | while (!left.empty()) { 44 | result.append(left.pop()); 45 | } 46 | System.out.println(result.reverse().toString()); 47 | } 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /Solutions/[02]_3.py: -------------------------------------------------------------------------------- 1 | test_case = int(input()) 2 | 3 | for _ in range(test_case): 4 | data = input() 5 | left_stack = [] 6 | right_stack = [] 7 | 8 | for i in data: 9 | if i == '-': 10 | if left_stack: 11 | left_stack.pop() 12 | elif i == '<': 13 | if left_stack: 14 | right_stack.append(left_stack.pop()) 15 | elif i == '>': 16 | if right_stack: 17 | left_stack.append(right_stack.pop()) 18 | else: 19 | left_stack.append(i) 20 | 21 | left_stack.extend(reversed(right_stack)) 22 | print(''.join(left_stack)) -------------------------------------------------------------------------------- /Solutions/[03]_1.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.InputStreamReader; 3 | import java.security.MessageDigest; 4 | 5 | public class Main { 6 | 7 | public static void main(String[] args) throws Exception { 8 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 9 | String base = br.readLine(); 10 | 11 | MessageDigest md = MessageDigest.getInstance("SHA-256"); 12 | byte[] hash = md.digest(base.getBytes("UTF-8")); 13 | StringBuffer result = new StringBuffer(); 14 | 15 | for (int i = 0; i < hash.length; i++) { 16 | String hex = Integer.toHexString(0xff & hash[i]); 17 | if (hex.length() == 1) result.append('0'); 18 | result.append(hex); 19 | } 20 | 21 | System.out.println(result.toString()); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Solutions/[03]_1.py: -------------------------------------------------------------------------------- 1 | import hashlib 2 | 3 | input_data = input() 4 | encoded_data = input_data.encode() 5 | result = hashlib.sha256(encoded_data).hexdigest() 6 | print(result) -------------------------------------------------------------------------------- /Solutions/[03]_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int n, m; 6 | set data; 7 | vector finding; 8 | 9 | int main() { 10 | cin >> n; 11 | for (int i = 0; i < n; i++) { 12 | int x; 13 | cin >> x; 14 | data.insert(x); 15 | } 16 | cin >> m; 17 | for (int i = 0; i < m; i++) { 18 | int x; 19 | cin >> x; 20 | finding.push_back(x); 21 | } 22 | for (auto x: finding) { 23 | if (data.count(x)) cout << "1\n"; 24 | else cout << "0\n"; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Solutions/[03]_2.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.HashSet; 3 | import java.util.Scanner; 4 | 5 | public class Main { 6 | 7 | public static void main(String[] args) throws Exception { 8 | Scanner sc = new Scanner(System.in); 9 | 10 | int n, m; 11 | HashSet set = new HashSet(); 12 | ArrayList finding = new ArrayList(); 13 | 14 | n = sc.nextInt(); 15 | for (int i = 0; i < n; i++) { 16 | set.add(sc.nextInt()); 17 | } 18 | m = sc.nextInt(); 19 | for (int i = 0; i < m; i++) { 20 | finding.add(sc.nextInt()); 21 | } 22 | for (int i = 0; i < m; i++) { 23 | if (set.contains(finding.get(i))) System.out.println("1"); 24 | else System.out.println("0"); 25 | } 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Solutions/[03]_2.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | array = set(map(int, input().split())) 3 | m = int(input()) 4 | x = list(map(int, input().split())) 5 | 6 | for i in x: 7 | if i not in array: 8 | print('0') 9 | else: 10 | print('1') -------------------------------------------------------------------------------- /Solutions/[03]_3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int tc; 6 | int parent[200001]; 7 | int number[200001]; 8 | char one[21], two[21]; 9 | 10 | int findParent(int x) { 11 | if (x == parent[x]) return x; 12 | return parent[x] = findParent(parent[x]); 13 | } 14 | 15 | void unionParent(int x, int y) { 16 | x = findParent(x); 17 | y = findParent(y); 18 | 19 | if (x != y) { 20 | parent[y] = x; 21 | number[x] += number[y]; 22 | } 23 | } 24 | 25 | int main() { 26 | cin >> tc; 27 | while(tc--) { 28 | map data; // <이름(키), 값> 29 | int f; 30 | int cnt = 1; 31 | 32 | cin >> f; 33 | for (int i = 1; i <= 2 * f; i++) { 34 | parent[i] = i; 35 | number[i] = 1; 36 | } 37 | 38 | for (int i = 0; i < f; i++) { 39 | string x, y; 40 | scanf("%s %s", &one, &two); // scanf()를 이용하여 시간 줄이기 41 | 42 | if (data.count(one) == 0) data[one] = cnt++; 43 | if (data.count(two) == 0) data[two] = cnt++; 44 | 45 | unionParent(data[one], data[two]); 46 | cout << number[findParent(data[one])] << '\n'; 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Solutions/[03]_3.py: -------------------------------------------------------------------------------- 1 | def find(x): 2 | if x == parent[x]: 3 | return x 4 | else: 5 | p = find(parent[x]) 6 | parent[x] = p 7 | return parent[x] 8 | 9 | def union(x, y): 10 | x = find(x) 11 | y = find(y) 12 | if x != y: 13 | parent[y] = x 14 | number[x] += number[y] 15 | 16 | test_case = int(input()) 17 | 18 | for _ in range(test_case): 19 | f = int(input()) 20 | parent = dict() 21 | number = dict() 22 | 23 | for _ in range(f): 24 | x, y = input().split(' ') 25 | if x not in parent: 26 | parent[x] = x 27 | number[x] = 1 28 | if y not in parent: 29 | parent[y] = y 30 | number[y] = 1 31 | 32 | union(x, y) 33 | print(number[find(x)]) -------------------------------------------------------------------------------- /Solutions/[04]_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int n; 6 | vector data; 7 | 8 | int main() { 9 | cin >> n; 10 | for (int i = 0; i < n; i++) { 11 | int x; 12 | scanf("%d", &x); // scanf()로 빠르게 입력 13 | data.push_back(x); 14 | } 15 | sort(data.begin(), data.end()); 16 | for (auto x: data) { 17 | cout << x << '\n'; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Solutions/[04]_1.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.InputStreamReader; 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | 6 | public class Main { 7 | 8 | public static void main(String[] args) throws Exception { 9 | // BufferedReader 클래스를 이용해 빠르게 입력 10 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 11 | 12 | int n = Integer.parseInt(br.readLine()); 13 | ArrayList data = new ArrayList(); 14 | 15 | for (int i = 0; i < n; i++) { 16 | int x = Integer.parseInt(br.readLine()); 17 | data.add(x); 18 | } 19 | 20 | Collections.sort(data); 21 | 22 | for (int x: data) { 23 | System.out.println(x); 24 | } 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Solutions/[04]_1.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | data = [] 4 | for _ in range(n): 5 | data.append(int(input())) 6 | data.sort() 7 | 8 | for x in data: 9 | print(x) -------------------------------------------------------------------------------- /Solutions/[04]_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | string n; 6 | 7 | int main() { 8 | cin >> n; 9 | for (int i = 9; i >= 0; i--) { 10 | for (char x: n) { 11 | if (x - '0' == i) { 12 | cout << i; 13 | } 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Solutions/[04]_2.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) throws Exception { 6 | Scanner sc = new Scanner(System.in); 7 | 8 | String n = sc.nextLine(); 9 | for (int i = 9; i >= 0; i--) { 10 | for (int j = 0; j < n.length(); j++) { 11 | if (n.charAt(j) - '0' == i) { 12 | System.out.print(i); 13 | } 14 | } 15 | } 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /Solutions/[04]_2.py: -------------------------------------------------------------------------------- 1 | array = input() 2 | 3 | for i in range(9, -1, -1): 4 | for j in array: 5 | if int(j) == i: 6 | print(i, end='') -------------------------------------------------------------------------------- /Solutions/[05]_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | bool compare (pair a, pair b) { 6 | return a.first < b.first; 7 | } 8 | 9 | int n; 10 | vector> data; 11 | 12 | int main() { 13 | cin >> n; 14 | for (int i = 0; i < n; i++) { 15 | int x; 16 | string y; 17 | cin >> x >> y; 18 | data.push_back(make_pair(x, y)); 19 | } 20 | stable_sort(data.begin(), data.end(), compare); 21 | for (auto x: data) { 22 | cout << x.first << " " << x.second << '\n'; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Solutions/[05]_1.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Collections; 3 | import java.util.Comparator; 4 | import java.util.Scanner; 5 | 6 | class Node { 7 | public int age; 8 | public String name; 9 | 10 | public Node(int age, String name) { 11 | this.age = age; 12 | this.name = name; 13 | } 14 | } 15 | 16 | public class Main { 17 | 18 | public static void main(String[] args) throws Exception { 19 | Scanner sc = new Scanner(System.in); 20 | 21 | int n = sc.nextInt(); 22 | ArrayList data = new ArrayList(); 23 | 24 | for (int i = 0; i < n; i++) { 25 | int x = sc.nextInt(); 26 | String y = sc.next(); 27 | data.add(new Node(x, y)); 28 | } 29 | 30 | Collections.sort(data, new Comparator() { 31 | @Override 32 | public int compare(Node a, Node b) { 33 | return Integer.compare(a.age, b.age); 34 | } 35 | }); 36 | 37 | for (Node node: data) { 38 | System.out.println(node.age + " " + node.name); 39 | } 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /Solutions/[05]_1.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | array = [] 4 | for _ in range(n): 5 | data = input().split(' ') 6 | array.append((int(data[0]), data[1])) 7 | array = sorted(array, key=lambda x: x[0]) 8 | 9 | for i in array: 10 | print(i[0], i[1]) -------------------------------------------------------------------------------- /Solutions/[05]_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int n; 6 | vector> data; 7 | 8 | int main() { 9 | cin >> n; 10 | for (int i = 0; i < n; i++) { 11 | int x, y; 12 | cin >> x >> y; 13 | data.push_back(make_pair(x, y)); 14 | } 15 | sort(data.begin(), data.end()); 16 | for (auto x: data) { 17 | cout << x.first << " " << x.second << '\n'; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Solutions/[05]_2.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Collections; 3 | import java.util.Comparator; 4 | import java.util.Scanner; 5 | 6 | class Node { 7 | public int x; 8 | public int y; 9 | 10 | public Node(int x, int y) { 11 | this.x = x; 12 | this.y = y; 13 | } 14 | } 15 | 16 | public class Main { 17 | 18 | public static void main(String[] args) throws Exception { 19 | Scanner sc = new Scanner(System.in); 20 | 21 | int n = sc.nextInt(); 22 | ArrayList data = new ArrayList(); 23 | 24 | for (int i = 0; i < n; i++) { 25 | int x = sc.nextInt(); 26 | int y = sc.nextInt(); 27 | data.add(new Node(x, y)); 28 | } 29 | 30 | Collections.sort(data, new Comparator() { 31 | @Override 32 | public int compare(Node a, Node b) { 33 | // x 좌표의 값이 같다면, y의 값을 기준으로 정렬 34 | if (a.x == b.x) { 35 | return Integer.compare(a.y, b.y); 36 | } 37 | // x 좌표의 값이 다르다면, x의 값을 기준으로 정렬 38 | return Integer.compare(a.x, b.x); 39 | } 40 | }); 41 | 42 | for (Node node: data) { 43 | System.out.println(node.x + " " + node.y); 44 | } 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /Solutions/[05]_2.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | array = [] 4 | for _ in range(n): 5 | x, y = map(int, input().split(' ')) 6 | array.append((x, y)) 7 | array = sorted(array) 8 | 9 | for i in array: 10 | print(i[0], i[1]) -------------------------------------------------------------------------------- /Solutions/[05]_3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int n; 6 | int data[10001]; 7 | 8 | int main() { 9 | cin >> n; 10 | for (int i = 0; i < n; i++) { 11 | int x; 12 | scanf("%d", &x); // scanf()로 빠르게 입력 13 | data[x]++; 14 | } 15 | for (int i = 0; i < 10001; i++) { 16 | if (data[i] != 0) { 17 | for (int j = 0; j < data[i]; j++) { 18 | cout << i << '\n'; 19 | } 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Solutions/[05]_3.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.InputStreamReader; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) throws Exception { 7 | // 빠르게 입력을 받기 위해 BufferedReader 라이브러리 사용 8 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 9 | 10 | int n = Integer.parseInt(br.readLine()); 11 | int[] data = new int[10001]; 12 | 13 | for (int i = 0; i < n; i++) { 14 | data[Integer.parseInt(br.readLine())]++; 15 | } 16 | 17 | // 한 번에 빠르게 출력하기 위해 StringBuilder 라이브러리 사용 18 | StringBuilder result = new StringBuilder(); 19 | 20 | for (int i = 0; i < 10001; i++) { 21 | if (data[i] != 0) { 22 | for (int j = 0; j < data[i]; j++) { 23 | result.append(i).append("\n"); 24 | } 25 | } 26 | } 27 | 28 | System.out.print(result); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Solutions/[05]_3.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | n = int(sys.stdin.readline()) 4 | array = [0] * 10001 5 | 6 | for i in range(n): 7 | data = int(sys.stdin.readline()) 8 | array[data] += 1 9 | 10 | for i in range(10001): 11 | if array[i] != 0: 12 | for j in range(array[i]): 13 | print(i) -------------------------------------------------------------------------------- /Solutions/[06]_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int n; 6 | 7 | int main() { 8 | cin >> n; 9 | 10 | int a = 0; 11 | int b = 1; 12 | while (n > 0) { 13 | int temp = a; 14 | a = b; 15 | b = temp + b; 16 | n--; 17 | } 18 | 19 | cout << a; 20 | } 21 | -------------------------------------------------------------------------------- /Solutions/[06]_1.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | Scanner sc = new Scanner(System.in); 7 | 8 | int n = sc.nextInt(); 9 | int a = 0; 10 | int b = 1; 11 | while (n > 0) { 12 | int temp = a; 13 | a = b; 14 | b = temp + b; 15 | n--; 16 | } 17 | 18 | System.out.println(a); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Solutions/[06]_1.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | a, b = 0, 1 4 | while n > 0: 5 | a, b = b, a + b 6 | n -= 1 7 | 8 | print(a) -------------------------------------------------------------------------------- /Solutions/[06]_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int result; 6 | int N, X, Y; 7 | 8 | void solve(int n, int x, int y) { 9 | if (n == 2) { 10 | if (x == X && y == Y) { 11 | cout << result; 12 | return; 13 | } 14 | result++; 15 | if (x == X && y + 1 == Y) { 16 | cout << result; 17 | return; 18 | } 19 | result++; 20 | if (x + 1 == X && y == Y) { 21 | cout << result; 22 | return; 23 | } 24 | result++; 25 | if (x + 1 == X && y + 1 == Y) { 26 | cout << result; 27 | return; 28 | } 29 | result++; 30 | return; 31 | } 32 | solve(n / 2, x, y); 33 | solve(n / 2, x, y + n / 2); 34 | solve(n / 2, x + n / 2, y); 35 | solve(n / 2, x + n / 2, y + n / 2); 36 | } 37 | 38 | int main() { 39 | cin >> N >> X >> Y; 40 | solve(pow(2, N), 0, 0); 41 | } 42 | -------------------------------------------------------------------------------- /Solutions/[06]_2.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | 5 | public static int N, X, Y, result = 0; 6 | 7 | public static void solve(int n, int x, int y) { 8 | if (n == 2) { 9 | if (x == X && y == Y) { 10 | System.out.println(result); 11 | return; 12 | } 13 | result++; 14 | if (x == X && y + 1 == Y) { 15 | System.out.println(result); 16 | return; 17 | } 18 | result++; 19 | if (x + 1 == X && y == Y) { 20 | System.out.println(result); 21 | return; 22 | } 23 | result++; 24 | if (x + 1 == X && y + 1 == Y) { 25 | System.out.println(result); 26 | return; 27 | } 28 | result++; 29 | return; 30 | } 31 | solve(n / 2, x, y); 32 | solve(n / 2, x, y + n / 2); 33 | solve(n / 2, x + n / 2, y); 34 | solve(n / 2, x + n / 2, y + n / 2); 35 | } 36 | 37 | public static void main(String[] args) { 38 | Scanner sc = new Scanner(System.in); 39 | 40 | N = sc.nextInt(); 41 | X = sc.nextInt(); 42 | Y = sc.nextInt(); 43 | 44 | solve((int) Math.pow(2, N), 0, 0); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /Solutions/[06]_2.py: -------------------------------------------------------------------------------- 1 | def solve(n, x, y): 2 | global result 3 | if n == 2: 4 | if x == X and y == Y: 5 | print(result) 6 | return 7 | result += 1 8 | if x == X and y + 1 == Y: 9 | print(result) 10 | return 11 | result += 1 12 | if x + 1 == X and y == Y: 13 | print(result) 14 | return 15 | result += 1 16 | if x + 1 == X and y + 1 == Y: 17 | print(result) 18 | return 19 | result += 1 20 | return 21 | solve(n / 2, x, y) 22 | solve(n / 2, x, y + n / 2) 23 | solve(n / 2, x + n / 2, y) 24 | solve(n / 2, x + n / 2, y + n / 2) 25 | 26 | result = 0 27 | N, X, Y = map(int, input().split(' ')) 28 | solve(2 ** N, 0, 0) -------------------------------------------------------------------------------- /Solutions/[06]_3.py: -------------------------------------------------------------------------------- 1 | import copy 2 | 3 | def recursive(array, n): 4 | if len(array) == n: 5 | operators_list.append(copy.deepcopy(array)) 6 | return 7 | array.append(' ') 8 | recursive(array, n) 9 | array.pop() 10 | array.append('+') 11 | recursive(array, n) 12 | array.pop() 13 | array.append('-') 14 | recursive(array, n) 15 | array.pop() 16 | 17 | test_case = int(input()) 18 | for _ in range(test_case): 19 | operators_list = [] 20 | n = int(input()) 21 | recursive([], n - 1) 22 | integers = [i for i in range(1, n + 1)] 23 | for operators in operators_list: 24 | string = "" 25 | for i in range(n - 1): 26 | string += str(integers[i]) + operators[i] 27 | string += str(integers[-1]) 28 | if eval(string.replace(" ", "")) == 0: 29 | print(string) 30 | print() -------------------------------------------------------------------------------- /Solutions/[07]_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int n; 6 | vector data; 7 | 8 | int main() { 9 | cin >> n; 10 | for (int i = 0; i < n; i++) { 11 | int x; 12 | scanf("%d", &x); // scanf()로 빠르게 입력 13 | data.push_back(x); 14 | } 15 | sort(data.begin(), data.end()); 16 | for (auto x: data) { 17 | cout << x << '\n'; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Solutions/[07]_1.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.InputStreamReader; 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | 6 | public class Main { 7 | 8 | public static void main(String[] args) throws Exception { 9 | // BufferedReader 클래스를 이용해 빠르게 입력 10 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 11 | 12 | int n = Integer.parseInt(br.readLine()); 13 | ArrayList data = new ArrayList(); 14 | 15 | for (int i = 0; i < n; i++) { 16 | int x = Integer.parseInt(br.readLine()); 17 | data.add(x); 18 | } 19 | 20 | Collections.sort(data); 21 | 22 | for (int x: data) { 23 | System.out.println(x); 24 | } 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Solutions/[07]_1.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | data = [] 4 | for _ in range(n): 5 | data.append(int(input())) 6 | data.sort() 7 | 8 | for x in data: 9 | print(x) -------------------------------------------------------------------------------- /Solutions/[07]_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int n, k; 6 | vector data; 7 | 8 | int main() { 9 | cin >> n >> k; 10 | for (int i = 0; i < n; i++) { 11 | int x; 12 | scanf("%d", &x); // scanf()로 빠르게 입력 13 | data.push_back(x); 14 | } 15 | sort(data.begin(), data.end()); 16 | cout << data[k - 1]; 17 | } 18 | -------------------------------------------------------------------------------- /Solutions/[07]_2.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.InputStreamReader; 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | import java.util.StringTokenizer; 6 | 7 | public class Main { 8 | 9 | public static void main(String[] args) throws Exception { 10 | // 빠르게 입력 받기 위해 BufferedReader 라이브러리 사용 11 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 12 | 13 | StringTokenizer st = new StringTokenizer(br.readLine()); 14 | int n = Integer.parseInt(st.nextToken()); 15 | int k = Integer.parseInt(st.nextToken()); 16 | 17 | ArrayList data = new ArrayList(); 18 | st = new StringTokenizer(br.readLine()); 19 | 20 | for (int i = 0; i < n; i++) { 21 | data.add(Integer.parseInt(st.nextToken())); 22 | } 23 | 24 | Collections.sort(data); 25 | System.out.println(data.get(k - 1)); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Solutions/[07]_2.py: -------------------------------------------------------------------------------- 1 | n, k = map(int, input().split()) 2 | array = list(map(int, input().split())) 3 | array = sorted(array) 4 | print(array[k - 1]) -------------------------------------------------------------------------------- /Solutions/[08]_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | string document, word; 6 | int idx, result; 7 | 8 | int main() { 9 | getline(cin, document); 10 | getline(cin, word); 11 | while (document.length() - idx >= word.length()) { 12 | if (document.substr(idx, word.length()) == word) { 13 | result += 1; 14 | idx += word.length(); 15 | } 16 | else { 17 | idx++; 18 | } 19 | } 20 | cout << result; 21 | } 22 | -------------------------------------------------------------------------------- /Solutions/[08]_1.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) throws Exception { 6 | Scanner sc = new Scanner(System.in); 7 | 8 | String document = sc.nextLine(); 9 | String word = sc.nextLine(); 10 | int idx = 0, result = 0; 11 | 12 | while (document.length() - idx >= word.length()) { 13 | if (document.substring(idx, idx + word.length()).equals(word)) { 14 | result += 1; 15 | idx += word.length(); 16 | } 17 | else { 18 | idx++; 19 | } 20 | } 21 | 22 | System.out.println(result); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Solutions/[08]_1.py: -------------------------------------------------------------------------------- 1 | document = input() 2 | word = input() 3 | index = 0 4 | result = 0 5 | 6 | while len(document) - index >= len(word): 7 | # 문서에서 보고 있는 단어가 찾고자 하는 단어인 경우 8 | if document[index:index + len(word)] == word: 9 | result += 1 10 | index += len(word) 11 | else: 12 | index += 1 13 | 14 | print(result) -------------------------------------------------------------------------------- /Solutions/[08]_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int n, result, k = 1; 6 | 7 | int main() { 8 | cin >> n; 9 | while (n != 0) { 10 | if (k > n) k = 1; 11 | n -= k; 12 | k += 1; 13 | result += 1; 14 | } 15 | cout << result; 16 | } 17 | -------------------------------------------------------------------------------- /Solutions/[08]_2.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) throws Exception { 6 | Scanner sc = new Scanner(System.in); 7 | int n = sc.nextInt(); 8 | int result = 0, k = 1; 9 | 10 | while (n != 0) { 11 | if (k > n) k = 1; 12 | n -= k; 13 | k += 1; 14 | result += 1; 15 | } 16 | 17 | System.out.println(result); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Solutions/[08]_2.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | result = 0 3 | k = 1 4 | 5 | while n != 0: # 모든 새가 날아갈 때까지 6 | if k > n: 7 | k = 1 8 | n -= k 9 | k += 1 10 | result += 1 11 | 12 | print(result) -------------------------------------------------------------------------------- /Solutions/[08]_3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int n; 6 | map books; 7 | vector result; 8 | 9 | int main() { 10 | cin >> n; 11 | for (int i = 0; i < n; i++) { 12 | string book; 13 | cin >> book; 14 | books[book]++; 15 | } 16 | 17 | int max_value = 0; 18 | for(auto it = books.begin(); it != books.end(); it++){ 19 | max_value = max(max_value, it->second); 20 | } 21 | 22 | for(auto it = books.begin(); it != books.end(); it++){ 23 | if (it->second == max_value) { 24 | result.push_back(it->first); 25 | } 26 | } 27 | 28 | cout << result[0]; 29 | } 30 | -------------------------------------------------------------------------------- /Solutions/[08]_3.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Collections; 3 | import java.util.HashMap; 4 | import java.util.Scanner; 5 | 6 | public class Main { 7 | 8 | public static void main(String[] args) throws Exception { 9 | Scanner sc = new Scanner(System.in); 10 | 11 | // 정수 이후에 문자열을 받는 경우, 다음과 같이 먼저 문자열로 입력 받아야 합니다. 12 | int n = Integer.parseInt(sc.nextLine()); 13 | HashMap books = new HashMap(); 14 | ArrayList result = new ArrayList(); 15 | int max_value = 0; 16 | 17 | for (int i = 0; i < n; i++) { 18 | String book = sc.nextLine(); 19 | // 해당 책이 출현한 빈도를 계산 20 | if (books.containsKey(book)) { 21 | books.put(book, books.get(book) + 1); 22 | } 23 | else { 24 | books.put(book, 1); 25 | } 26 | max_value = Math.max(max_value, books.get(book)); 27 | } 28 | 29 | // 빈도수가 가장 높은 책의 이름들을 저장 30 | for (String book: books.keySet()) { 31 | if (books.get(book) == max_value) result.add(book); 32 | } 33 | Collections.sort(result); 34 | System.out.println(result.get(0)); 35 | 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Solutions/[08]_3.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | books = {} 3 | 4 | for _ in range(n): 5 | book = input() 6 | if book not in books: 7 | books[book] = 1 8 | else: 9 | books[book] += 1 10 | 11 | target = max(books.values()) 12 | array = [] 13 | for book, number in books.items(): 14 | if number == target: 15 | array.append(book) 16 | 17 | print(sorted(array)[0]) -------------------------------------------------------------------------------- /Solutions/[08]_4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int n; 6 | vector data; 7 | 8 | int ascending() { 9 | int now = data[0]; 10 | int result = 1; 11 | for (int i = 1; i < data.size(); i++) { 12 | if (now < data[i]) { 13 | result += 1; 14 | now = data[i]; 15 | } 16 | } 17 | return result; 18 | } 19 | 20 | int main() { 21 | cin >> n; 22 | for (int i = 0; i < n; i++) { 23 | int x; 24 | cin >> x; 25 | data.push_back(x); 26 | } 27 | 28 | cout << ascending() << '\n'; 29 | reverse(data.begin(), data.end()); 30 | cout << ascending() << '\n'; 31 | } 32 | -------------------------------------------------------------------------------- /Solutions/[08]_4.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Collections; 3 | import java.util.Scanner; 4 | 5 | public class Main { 6 | 7 | public static ArrayList data = new ArrayList(); 8 | 9 | public static int ascending() { 10 | int now = data.get(0); 11 | int result = 1; 12 | for (int i = 1; i < data.size(); i++) { 13 | if (now < data.get(i)) { 14 | result += 1; 15 | now = data.get(i); 16 | } 17 | } 18 | return result; 19 | } 20 | 21 | public static void main(String[] args) throws Exception { 22 | Scanner sc = new Scanner(System.in); 23 | 24 | int n = Integer.parseInt(sc.nextLine()); 25 | for (int i = 0; i < n; i++) { 26 | int x = Integer.parseInt(sc.nextLine()); 27 | data.add(x); 28 | } 29 | 30 | System.out.println(ascending()); 31 | Collections.reverse(data); 32 | System.out.println(ascending()); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Solutions/[08]_4.py: -------------------------------------------------------------------------------- 1 | def ascending(array): 2 | now = array[0] 3 | result = 1 4 | for i in range(1, len(array)): 5 | if now < array[i]: 6 | result += 1 7 | now = array[i] 8 | return result 9 | 10 | n = int(input()) 11 | array = [] 12 | 13 | for _ in range(n): 14 | array.append(int(input())) 15 | 16 | print(ascending(array)) 17 | array.reverse() 18 | print(ascending(array)) -------------------------------------------------------------------------------- /Solutions/[08]_5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int n, m; 6 | char data[50][50]; 7 | int row[50], column[50]; 8 | 9 | int main() { 10 | cin >> n >> m; 11 | for (int i = 0; i < n; i++) { 12 | for (int j = 0; j < m; j++) { 13 | cin >> data[i][j]; 14 | if (data[i][j] == 'X') { 15 | row[i] = 1; 16 | column[j] = 1; 17 | } 18 | } 19 | } 20 | 21 | int row_count = 0; 22 | for (int i = 0; i < n; i++) { 23 | if (row[i] == 0) row_count++; 24 | } 25 | 26 | int column_count = 0; 27 | for (int j = 0; j < m; j++) { 28 | if (column[j] == 0) column_count++; 29 | } 30 | 31 | cout << max(row_count, column_count); 32 | } 33 | -------------------------------------------------------------------------------- /Solutions/[08]_5.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | Scanner sc = new Scanner(System.in); 7 | 8 | String input[] = sc.nextLine().split(" "); 9 | int n = Integer.parseInt(input[0]); 10 | int m = Integer.parseInt(input[1]); 11 | 12 | char[][] data = new char[50][50]; 13 | int[] row = new int[50]; 14 | int[] column = new int[50]; 15 | 16 | for (int i = 0; i < n; i++) { 17 | data[i] = sc.nextLine().toCharArray(); 18 | for (int j = 0; j < m; j++) { 19 | if (data[i][j] == 'X') { 20 | row[i] = 1; 21 | column[j] = 1; 22 | } 23 | } 24 | } 25 | 26 | int row_count = 0; 27 | for (int i = 0; i < n; i++) { 28 | if (row[i] == 0) row_count++; 29 | } 30 | 31 | int column_count = 0; 32 | for (int j = 0; j < m; j++) { 33 | if (column[j] == 0) column_count++; 34 | } 35 | 36 | System.out.println(Math.max(row_count, column_count)); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Solutions/[08]_5.py: -------------------------------------------------------------------------------- 1 | n, m = map(int, input().split()) 2 | array = [] 3 | 4 | for _ in range(n): 5 | array.append(input()) 6 | 7 | row = [0] * n 8 | column = [0] * m 9 | 10 | for i in range(n): 11 | for j in range(m): 12 | if array[i][j] == 'X': 13 | row[i] = 1 14 | column[j] = 1 15 | 16 | row_count = 0 17 | for i in range(n): 18 | if row[i] == 0: 19 | row_count += 1 20 | 21 | column_count = 0 22 | for j in range(m): 23 | if column[j] == 0: 24 | column_count += 1 25 | 26 | print(max(row_count, column_count)) -------------------------------------------------------------------------------- /Solutions/[09]_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int n, c; 6 | vector data; 7 | 8 | int main() { 9 | cin >> n >> c; 10 | for (int i = 0; i < n; i++) { 11 | int x; 12 | cin >> x; 13 | data.push_back(x); 14 | } 15 | 16 | sort(data.begin(), data.end()); 17 | 18 | int start = data[1] - data[0]; 19 | int end = data[n - 1] - data[0]; 20 | int result = 0; 21 | 22 | while (start <= end) { 23 | int mid = (start + end) / 2; 24 | int value = data[0]; 25 | int cnt = 1; 26 | for (int i = 1; i < n; i++) { 27 | if (data[i] >= value + mid) { 28 | value = data[i]; 29 | cnt += 1; 30 | } 31 | } 32 | if (cnt >= c) { 33 | start = mid + 1; 34 | result = mid; 35 | } 36 | else { 37 | end = mid - 1; 38 | } 39 | } 40 | 41 | cout << result; 42 | } 43 | -------------------------------------------------------------------------------- /Solutions/[09]_1.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Collections; 3 | import java.util.Scanner; 4 | 5 | public class Main { 6 | 7 | public static void main(String[] args) { 8 | Scanner sc = new Scanner(System.in); 9 | 10 | int n = sc.nextInt(); 11 | int c = sc.nextInt(); 12 | ArrayList data = new ArrayList(); 13 | 14 | for (int i = 0; i < n; i++) { 15 | int x = sc.nextInt(); 16 | data.add(x); 17 | } 18 | 19 | Collections.sort(data); 20 | 21 | int start = data.get(1) - data.get(0); 22 | int end = data.get(n - 1) - data.get(0); 23 | int result = 0; 24 | 25 | while (start <= end) { 26 | int mid = (start + end) / 2; 27 | int value = data.get(0); 28 | int cnt = 1; 29 | for (int i = 1; i < n; i++) { 30 | if (data.get(i) >= value + mid) { 31 | value = data.get(i); 32 | cnt += 1; 33 | } 34 | } 35 | if (cnt >= c) { 36 | start = mid + 1; 37 | result = mid; 38 | } 39 | else { 40 | end = mid - 1; 41 | } 42 | } 43 | 44 | System.out.println(result); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /Solutions/[09]_1.py: -------------------------------------------------------------------------------- 1 | n, c = list(map(int, input().split(' '))) 2 | 3 | data = [] 4 | for _ in range(n): 5 | data.append(int(input())) 6 | data.sort() 7 | 8 | start = data[1] - data[0] 9 | end = data[-1] - data[0] 10 | result = 0 11 | 12 | while(start <= end): 13 | mid = (start + end) // 2 # mid는 가장 인접한 두 공유기 사이의 거리(Gap)을 의미합니다. 14 | value = data[0] 15 | count = 1 16 | for i in range(1, len(data)): # 현재의 mid 값을 이용해 공유기를 설치 해보기 17 | if data[i] >= value + mid: 18 | value = data[i] 19 | count += 1 20 | if count >= c: # C개 이상의 공유기를 설치할 수 있는 경우, 거리를 증가시킵니다. 21 | start = mid + 1 22 | result = mid # 최적의 결과를 저장합니다. 23 | else: # C개 이상의 공유기를 설치할 수 없는 경우, 거리를 감소시킵니다. 24 | end = mid - 1 25 | 26 | print(result) -------------------------------------------------------------------------------- /Solutions/[09]_2.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | 3 | n, m = map(int, input().split()) 4 | adj = [[] for _ in range(n + 1)] 5 | 6 | def bfs(c): 7 | queue = deque([start_node]) 8 | visited = [False] * (n + 1) 9 | visited[start_node] = True 10 | 11 | while queue: 12 | x = queue.popleft() 13 | for y, weight in adj[x]: 14 | if not visited[y] and weight >= c: 15 | visited[y] = True 16 | queue.append(y) 17 | 18 | return visited[end_node] 19 | 20 | start = 1000000000 21 | end = 1 22 | 23 | for _ in range(m): 24 | x, y, weight = map(int, input().split()) 25 | adj[x].append((y, weight)) 26 | adj[y].append((x, weight)) 27 | start = min(start, weight) 28 | end = max(end, weight) 29 | 30 | start_node, end_node = map(int, input().split()) 31 | 32 | result = start 33 | while(start <= end): 34 | mid = (start + end) // 2 # mid는 현재의 중량을 의미합니다. 35 | if bfs(mid): # 이동이 가능하므로, 중량을 증가시킵니다. 36 | result = mid 37 | start = mid + 1 38 | else: # 이동이 불가능하므로, 중량을 감소시킵니다. 39 | end = mid - 1 40 | 41 | print(result) -------------------------------------------------------------------------------- /Solutions/[10]_1.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self, data, left_node, right_node): 3 | self.data = data 4 | self.left_node = left_node 5 | self.right_node = right_node 6 | 7 | def pre_order(node): 8 | print(node.data, end='') 9 | if node.left_node != '.': 10 | pre_order(tree[node.left_node]) 11 | if node.right_node != '.': 12 | pre_order(tree[node.right_node]) 13 | 14 | def in_order(node): 15 | if node.left_node != '.': 16 | in_order(tree[node.left_node]) 17 | print(node.data, end='') 18 | if node.right_node != '.': 19 | in_order(tree[node.right_node]) 20 | 21 | def post_order(node): 22 | if node.left_node != '.': 23 | post_order(tree[node.left_node]) 24 | if node.right_node != '.': 25 | post_order(tree[node.right_node]) 26 | print(node.data, end='') 27 | 28 | n = int(input()) 29 | tree = {} 30 | 31 | for i in range(n): 32 | data, left_node, right_node = input().split() 33 | tree[data] = Node(data, left_node, right_node) 34 | 35 | pre_order(tree['A']) 36 | print() 37 | in_order(tree['A']) 38 | print() 39 | post_order(tree['A']) -------------------------------------------------------------------------------- /Solutions/[10]_2.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self, number, left_node, right_node): 3 | self.parent = -1 4 | self.number = number 5 | self.left_node = left_node 6 | self.right_node = right_node 7 | 8 | def in_order(node, level): 9 | global level_depth, x 10 | level_depth = max(level_depth, level) 11 | if node.left_node != -1: 12 | in_order(tree[node.left_node], level + 1) 13 | level_min[level] = min(level_min[level], x) 14 | level_max[level] = max(level_max[level], x) 15 | x += 1 16 | if node.right_node != -1: 17 | in_order(tree[node.right_node], level + 1) 18 | 19 | n = int(input()) 20 | tree = {} 21 | level_min = [n] 22 | level_max = [0] 23 | root = -1 24 | x = 1 25 | level_depth = 1 26 | 27 | for i in range(1, n + 1): 28 | tree[i] = Node(i, -1, -1) 29 | level_min.append(n) 30 | level_max.append(0) 31 | 32 | for _ in range(n): 33 | number, left_node, right_node = map(int, input().split()) 34 | tree[number].left_node = left_node 35 | tree[number].right_node = right_node 36 | if left_node != -1: 37 | tree[left_node].parent = number 38 | if right_node != -1: 39 | tree[right_node].parent = number 40 | 41 | for i in range(1, n + 1): 42 | if tree[i].parent == -1: 43 | root = i 44 | 45 | in_order(tree[root], 1) 46 | result_level = 1 47 | result_width = level_max[1] - level_min[1] + 1 48 | for i in range(2, level_depth + 1): 49 | width = level_max[i] - level_min[i] + 1 50 | if result_width < width: 51 | result_level = i 52 | result_width = width 53 | 54 | print(result_level, result_width) -------------------------------------------------------------------------------- /Solutions/[11]_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int n; 6 | // C++에서는 기본적으로 우선순위 큐가 최대 힙(Max Heap) 7 | priority_queue pq; 8 | vector result; 9 | 10 | int main(void) { 11 | cin >> n; 12 | 13 | for (int i = 0; i < n; i++) { 14 | int x; 15 | cin >> x; 16 | if (x == 0) { 17 | if (!pq.empty()) { 18 | result.push_back(-pq.top()); 19 | pq.pop(); 20 | } 21 | else { 22 | result.push_back(0); 23 | } 24 | } 25 | else { 26 | pq.push(-x); 27 | } 28 | } 29 | 30 | for (int i = 0; i < result.size(); i++) { 31 | cout << result[i] << '\n'; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Solutions/[11]_1.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.PriorityQueue; 3 | import java.util.Scanner; 4 | 5 | public class Main { 6 | 7 | public static void main(String[] args) { 8 | Scanner sc = new Scanner(System.in); 9 | 10 | int n = sc.nextInt(); 11 | // 자바(Java)에서는 기본적으로 우선순위 큐가 최소 힙(Min Heap) 12 | PriorityQueue pq = new PriorityQueue(); 13 | ArrayList result = new ArrayList(); 14 | 15 | for (int i = 0; i < n; i++) { 16 | int x = sc.nextInt(); 17 | if (x == 0) { 18 | if (!pq.isEmpty()) { 19 | result.add(pq.poll()); 20 | } 21 | else { 22 | result.add(0); 23 | } 24 | } 25 | else { 26 | pq.add(x); 27 | } 28 | } 29 | 30 | for (int i = 0; i < result.size(); i++) { 31 | System.out.println(result.get(i)); 32 | } 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Solutions/[11]_1.py: -------------------------------------------------------------------------------- 1 | import heapq 2 | 3 | n = int(input()) 4 | heap = [] 5 | result = [] 6 | 7 | for _ in range(n): 8 | data = int(input()) 9 | if data == 0: 10 | if heap: 11 | result.append(heapq.heappop(heap)) 12 | else: 13 | result.append(0) 14 | else: 15 | heapq.heappush(heap, data) 16 | 17 | for data in result: 18 | print(data) -------------------------------------------------------------------------------- /Solutions/[11]_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int n, result; 6 | // C++에서는 기본적으로 우선순위 큐가 최대 힙(Max Heap) 7 | priority_queue pq; 8 | 9 | int main(void) { 10 | cin >> n; 11 | 12 | for (int i = 0; i < n; i++) { 13 | int x; 14 | cin >> x; 15 | pq.push(-x); 16 | } 17 | 18 | while (pq.size() != 1) { 19 | int one = pq.top(); 20 | pq.pop(); 21 | int two = pq.top(); 22 | pq.pop(); 23 | int sum_value = one + two; 24 | result += sum_value; 25 | pq.push(sum_value); 26 | } 27 | 28 | cout << -result; 29 | } 30 | -------------------------------------------------------------------------------- /Solutions/[11]_2.java: -------------------------------------------------------------------------------- 1 | import java.util.PriorityQueue; 2 | import java.util.Scanner; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | Scanner sc = new Scanner(System.in); 8 | 9 | int n = sc.nextInt(); 10 | int result = 0; 11 | // 자바(Java)에서는 기본적으로 우선순위 큐가 최소 힙(Min Heap) 12 | PriorityQueue pq = new PriorityQueue(); 13 | 14 | for (int i = 0; i < n; i++) { 15 | int x = sc.nextInt(); 16 | pq.add(x); 17 | } 18 | 19 | while (pq.size() != 1) { 20 | int one = pq.poll(); 21 | int two = pq.poll(); 22 | int sum_value = one + two; 23 | result += sum_value; 24 | pq.add(sum_value); 25 | } 26 | 27 | System.out.println(result); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Solutions/[11]_2.py: -------------------------------------------------------------------------------- 1 | import heapq 2 | 3 | n = int(input()) 4 | heap = [] 5 | 6 | for i in range(n): 7 | data = int(input()) 8 | heapq.heappush(heap, data) 9 | 10 | result = 0 11 | 12 | while len(heap) != 1: 13 | one = heapq.heappop(heap) 14 | two = heapq.heappop(heap) 15 | sum_value = one + two 16 | result += sum_value 17 | heapq.heappush(heap, sum_value) 18 | 19 | print(result) -------------------------------------------------------------------------------- /Solutions/[11]_3.py: -------------------------------------------------------------------------------- 1 | import heapq 2 | 3 | n, m = map(int, input().split()) 4 | array = [[] for i in range(n + 1)] 5 | indegree = [0] * (n + 1) 6 | heap = [] 7 | result = [] 8 | 9 | for _ in range(m): 10 | x, y = map(int, input().split()) 11 | array[x].append(y) 12 | indegree[y] += 1 13 | 14 | for i in range(1, n + 1): 15 | if indegree[i] == 0: 16 | heapq.heappush(heap, i) 17 | 18 | result = [] 19 | while heap: 20 | data = heapq.heappop(heap) 21 | result.append(data) 22 | for y in array[data]: 23 | indegree[y] -= 1 24 | if indegree[y] == 0: 25 | heapq.heappush(heap, y) 26 | 27 | for i in result: 28 | print(i, end=' ') -------------------------------------------------------------------------------- /Solutions/[12]_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int n; 6 | int dp[1000001]; 7 | 8 | int main() { 9 | cin >> n; 10 | dp[1] = 1; 11 | dp[2] = 2; 12 | for (int i = 3; i <= n; i++) { 13 | dp[i] = (dp[i - 2] + dp[i - 1]) % 15746; 14 | } 15 | cout << dp[n]; 16 | } 17 | -------------------------------------------------------------------------------- /Solutions/[12]_1.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | Scanner sc = new Scanner(System.in); 7 | 8 | int n = sc.nextInt(); 9 | int[] dp = new int[1000001]; 10 | 11 | dp[1] = 1; 12 | dp[2] = 2; 13 | 14 | for (int i = 3; i <= n; i++) { 15 | dp[i] = (dp[i - 2] + dp[i - 1]) % 15746; 16 | } 17 | 18 | System.out.println(dp[n]); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Solutions/[12]_1.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | dp = [0] * 1000001 4 | dp[1] = 1 5 | dp[2] = 2 6 | for i in range(3, n + 1): 7 | dp[i] = (dp[i - 2] + dp[i - 1]) % 15746 8 | 9 | print(dp[n]) -------------------------------------------------------------------------------- /Solutions/[12]_2.cpp: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | Scanner sc = new Scanner(System.in); 7 | 8 | int n = sc.nextInt(); 9 | int k = sc.nextInt(); 10 | int[][] dp = new int[n + 1][k + 1]; 11 | 12 | for (int i = 1; i <= n; i++) { 13 | int weight = sc.nextInt(); 14 | int value = sc.nextInt(); 15 | for (int j = 1; j <= k; j++) { 16 | if (j < weight) { 17 | dp[i][j] = dp[i - 1][j]; 18 | } 19 | else { 20 | dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - weight] + value); 21 | } 22 | } 23 | } 24 | 25 | System.out.println(dp[n][k]); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Solutions/[12]_2.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | Scanner sc = new Scanner(System.in); 7 | 8 | int n = sc.nextInt(); 9 | int k = sc.nextInt(); 10 | int[][] dp = new int[n + 1][k + 1]; 11 | 12 | for (int i = 1; i <= n; i++) { 13 | int weight = sc.nextInt(); 14 | int value = sc.nextInt(); 15 | for (int j = 1; j <= k; j++) { 16 | if (j < weight) { 17 | dp[i][j] = dp[i - 1][j]; 18 | } 19 | else { 20 | dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - weight] + value); 21 | } 22 | } 23 | } 24 | 25 | System.out.println(dp[n][k]); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Solutions/[12]_2.py: -------------------------------------------------------------------------------- 1 | n, k = map(int, input().split()) 2 | 3 | dp = [[0] * (k + 1) for _ in range(n + 1)] 4 | for i in range(1, n + 1): 5 | weight, value = map(int, input().split()) 6 | for j in range(1, k + 1): 7 | if j < weight: 8 | dp[i][j] = dp[i - 1][j] 9 | else: 10 | dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight] + value) 11 | 12 | print(dp[n][k]) -------------------------------------------------------------------------------- /Solutions/[12]_3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int n, result; 6 | vector data; 7 | int dp[1000]; 8 | 9 | int main() { 10 | cin >> n; 11 | for (int i = 0; i < n; i++) { 12 | int x; 13 | cin >> x; 14 | data.push_back(x); 15 | } 16 | for (int i = 0; i < n; i++) { 17 | dp[i] = 1; 18 | for (int j = 0; j < i; j++) { 19 | if (data[j] < data[i]) { 20 | dp[i] = max(dp[i], dp[j] + 1); 21 | } 22 | } 23 | result = max(result, dp[i]); 24 | } 25 | cout << result; 26 | } 27 | -------------------------------------------------------------------------------- /Solutions/[12]_3.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Scanner; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | Scanner sc = new Scanner(System.in); 8 | 9 | int n = sc.nextInt(); 10 | int result = 0; 11 | ArrayList data = new ArrayList(); 12 | int[] dp = new int[1000]; 13 | 14 | for (int i = 0; i < n; i++) { 15 | data.add(sc.nextInt()); 16 | } 17 | for (int i = 0; i < n; i++) { 18 | dp[i] = 1; 19 | for (int j = 0; j < i; j++) { 20 | if (data.get(j) < data.get(i)) { 21 | dp[i] = Math.max(dp[i], dp[j] + 1); 22 | } 23 | } 24 | result = Math.max(result, dp[i]); 25 | } 26 | System.out.println(result); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Solutions/[12]_3.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | array = list(map(int, input().split())) 3 | 4 | dp = [1] * n 5 | for i in range(1, n): 6 | for j in range(0, i): 7 | if array[j] < array[i]: 8 | dp[i] = max(dp[i], dp[j] + 1) 9 | 10 | print(max(dp)) -------------------------------------------------------------------------------- /Solutions/[13]_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | string x, y; 6 | int dp[1001][1001]; 7 | 8 | int main() { 9 | cin >> x >> y; 10 | 11 | for (int i = 1; i <= x.length(); i++) { 12 | for (int j = 1; j <= y.length(); j++) { 13 | if (x[i - 1] == y[j - 1]) { 14 | dp[i][j] = dp[i - 1][j - 1] + 1; 15 | } 16 | else { 17 | dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]); 18 | } 19 | } 20 | } 21 | 22 | cout << dp[x.length()][y.length()]; 23 | } 24 | -------------------------------------------------------------------------------- /Solutions/[13]_1.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | Scanner sc = new Scanner(System.in); 7 | 8 | String x = sc.nextLine(); 9 | String y = sc.nextLine(); 10 | int[][] dp = new int[1001][1001]; 11 | 12 | for (int i = 1; i <= x.length(); i++) { 13 | for (int j = 1; j <= y.length(); j++) { 14 | if (x.charAt(i - 1) == y.charAt(j - 1)) { 15 | dp[i][j] = dp[i - 1][j - 1] + 1; 16 | } 17 | else { 18 | dp[i][j] = Math.max(dp[i][j - 1], dp[i - 1][j]); 19 | } 20 | } 21 | } 22 | 23 | System.out.println(dp[x.length()][y.length()]); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Solutions/[13]_1.py: -------------------------------------------------------------------------------- 1 | x = input() 2 | y = input() 3 | 4 | dp = [[0] * (len(y) + 1) for _ in range(len(x) + 1)] 5 | for i in range(1, len(x) + 1): 6 | for j in range(1, len(y) + 1): 7 | if x[i - 1] == y[j - 1]: 8 | dp[i][j] = dp[i - 1][j - 1] + 1 9 | else: 10 | dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]) 11 | 12 | print(dp[len(x)][len(y)]) -------------------------------------------------------------------------------- /Solutions/[13]_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int n, s, m; 6 | vector data; 7 | int dp[101][1001]; 8 | 9 | int main() { 10 | cin >> n >> s >> m; 11 | for (int i = 0; i < n; i++) { 12 | int x; 13 | cin >> x; 14 | data.push_back(x); 15 | } 16 | 17 | dp[0][s] = 1; 18 | for (int i = 1; i <= n; i++) { 19 | for (int j = 0; j <= m; j++) { 20 | if (dp[i - 1][j] == 0) continue; 21 | if (j - data[i - 1] >= 0) { 22 | dp[i][j - data[i - 1]] = 1; 23 | } 24 | if (j + data[i - 1] <= m) { 25 | dp[i][j + data[i - 1]] = 1; 26 | } 27 | } 28 | } 29 | 30 | int result = -1; 31 | for (int i = m; i >= 0; i--) { 32 | if (dp[n][i] == 1) { 33 | result = i; 34 | break; 35 | } 36 | } 37 | 38 | cout << result; 39 | } 40 | -------------------------------------------------------------------------------- /Solutions/[13]_2.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Scanner; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | Scanner sc = new Scanner(System.in); 8 | 9 | int n = sc.nextInt(); 10 | int s = sc.nextInt(); 11 | int m = sc.nextInt(); 12 | ArrayList data = new ArrayList(); 13 | int[][] dp = new int[101][1001]; 14 | 15 | for (int i = 0; i < n; i++) { 16 | data.add(sc.nextInt()); 17 | } 18 | 19 | dp[0][s] = 1; 20 | for (int i = 1; i <= n; i++) { 21 | for (int j = 0; j <= m; j++) { 22 | if (dp[i - 1][j] == 0) continue; 23 | if (j - data.get(i - 1) >= 0) { 24 | dp[i][j - data.get(i - 1)] = 1; 25 | } 26 | if (j + data.get(i - 1) <= m) { 27 | dp[i][j + data.get(i - 1)] = 1; 28 | } 29 | } 30 | } 31 | 32 | int result = -1; 33 | for (int i = m; i >= 0; i--) { 34 | if (dp[n][i] == 1) { 35 | result = i; 36 | break; 37 | } 38 | } 39 | 40 | System.out.println(result); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /Solutions/[13]_2.py: -------------------------------------------------------------------------------- 1 | n, s, m = map(int, input().split()) 2 | array = list(map(int, input().split())) 3 | 4 | dp = [[0] * (m + 1) for _ in range(n + 1)] 5 | dp[0][s] = 1 6 | for i in range(1, n + 1): 7 | for j in range(m + 1): 8 | if dp[i - 1][j] == 0: 9 | continue 10 | if j - array[i - 1] >= 0: 11 | dp[i][j - array[i - 1]] = 1 12 | if j + array[i - 1] <= m: 13 | dp[i][j + array[i - 1]] = 1 14 | 15 | result = -1 16 | 17 | for i in range(m, -1, -1): 18 | if dp[n][i] == 1: 19 | result = i 20 | break 21 | 22 | print(result) -------------------------------------------------------------------------------- /Solutions/[13]_3.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | array = [] 4 | array.append((0, 0, 0, 0)) 5 | for i in range(1, n + 1): 6 | area, height, weight = map(int, input().split()) 7 | array.append((i, area, height, weight)) 8 | 9 | # 무게를 기준으로 정렬합니다. 10 | array.sort(key=lambda data: data[3]) 11 | 12 | dp = [0] * (n + 1) 13 | for i in range(1, n + 1): 14 | for j in range(0, i): 15 | if array[i][1] > array[j][1]: 16 | dp[i] = max(dp[i], dp[j] + array[i][2]) 17 | 18 | max_value = max(dp) 19 | index = n 20 | result = [] 21 | while index != 0: 22 | if max_value == dp[index]: 23 | result.append(array[index][0]) 24 | max_value -= array[index][2] 25 | index -= 1 26 | result.reverse() 27 | 28 | print(len(result)) 29 | [print(i) for i in result] -------------------------------------------------------------------------------- /Solutions/[14]_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int n, m, start; 6 | vector adj[1001]; 7 | bool d[1001]; 8 | 9 | // 깊이 우선 탐색(DFS) 10 | void dfs(int x) { 11 | if (d[x]) return; 12 | d[x] = true; 13 | cout << x << ' '; 14 | for (int i = 0; i < adj[x].size(); i++) { 15 | int y = adj[x][i]; 16 | dfs(y); 17 | } 18 | } 19 | 20 | // 너비 우선 탐색(BFS) 21 | void bfs(int x) { 22 | queue q; 23 | q.push(x); 24 | d[x] = true; 25 | while (!q.empty()) { 26 | int now = q.front(); 27 | q.pop(); 28 | cout << now << ' '; 29 | for (int i = 0; i < adj[now].size(); i++) { 30 | int y = adj[now][i]; 31 | if (!d[y]) { 32 | q.push(y); 33 | d[y] = true; 34 | } 35 | } 36 | } 37 | } 38 | 39 | int main() { 40 | cin >> n >> m >> start; 41 | for (int i = 0; i < m; i++) { 42 | int x, y; 43 | cin >> x >> y; 44 | adj[x].push_back(y); 45 | adj[y].push_back(x); 46 | } 47 | for(int i = 1; i <= n; i++) { 48 | sort(adj[i].begin(), adj[i].end()); 49 | } 50 | dfs(start); 51 | cout << '\n'; 52 | memset(d, false, sizeof(d)); 53 | bfs(start); 54 | } 55 | -------------------------------------------------------------------------------- /Solutions/[14]_1.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Collections; 3 | import java.util.LinkedList; 4 | import java.util.Queue; 5 | import java.util.Scanner; 6 | 7 | public class Main { 8 | 9 | public static int n, m, start; 10 | public static ArrayList> adj = new ArrayList<>(); 11 | public static boolean[] d = new boolean[1001]; 12 | 13 | // 깊이 우선 탐색(DFS) 14 | public static void dfs(int x) { 15 | if (d[x]) return; 16 | d[x] = true; 17 | System.out.print(x + " "); 18 | for (int i = 0; i < adj.get(x).size(); i++) { 19 | int y = adj.get(x).get(i); 20 | dfs(y); 21 | } 22 | } 23 | 24 | // 너비 우선 탐색(BFS) 25 | public static void bfs(int x) { 26 | Queue q = new LinkedList(); 27 | q.offer(x); 28 | d[x] = true; 29 | while (!q.isEmpty()) { 30 | int now = q.poll(); 31 | System.out.print(now + " "); 32 | for (int i = 0; i < adj.get(now).size(); i++) { 33 | int y = adj.get(now).get(i); 34 | if (!d[y]) { 35 | q.offer(y); 36 | d[y] = true; 37 | } 38 | } 39 | } 40 | } 41 | public static void main(String[] args) { 42 | Scanner sc = new Scanner(System.in); 43 | 44 | n = sc.nextInt(); 45 | m = sc.nextInt(); 46 | start = sc.nextInt(); 47 | 48 | for (int i = 0; i <= n; i++) { 49 | adj.add(new ArrayList()); 50 | } 51 | 52 | for (int i = 0; i < m; i++) { 53 | int x = sc.nextInt(); 54 | int y = sc.nextInt(); 55 | adj.get(x).add(y); 56 | adj.get(y).add(x); 57 | } 58 | 59 | for(int i = 1; i <= n; i++) { 60 | Collections.sort(adj.get(i)); 61 | } 62 | 63 | dfs(start); 64 | System.out.println(); 65 | d = new boolean[1001]; 66 | bfs(start); 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /Solutions/[14]_1.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | 3 | def dfs(v): 4 | print(v, end=' ') 5 | visited[v] = True 6 | for e in adj[v]: 7 | if not(visited[e]): 8 | dfs(e) 9 | 10 | def bfs(v): 11 | q = deque([v]) 12 | while q: 13 | v = q.popleft() 14 | if not(visited[v]): 15 | visited[v] = True 16 | print(v, end=' ') 17 | for e in adj[v]: 18 | if not visited[e]: 19 | q.append(e) 20 | 21 | n, m, v = map(int, input().split()) 22 | adj = [[] for _ in range(n + 1)] 23 | for _ in range(m): 24 | x, y = map(int, input().split()) 25 | adj[x].append(y) 26 | adj[y].append(x) 27 | 28 | for e in adj: 29 | e.sort() 30 | 31 | visited = [False] * (n + 1) 32 | dfs(v) 33 | print() 34 | visited = [False] * (n + 1) 35 | bfs(v) -------------------------------------------------------------------------------- /Solutions/[14]_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 100001 3 | 4 | using namespace std; 5 | 6 | int n, k; 7 | int data[MAX]; 8 | 9 | // 너비 우선 탐색(BFS) 10 | int bfs() { 11 | queue q; 12 | q.push(n); 13 | while (!q.empty()) { 14 | int now = q.front(); 15 | q.pop(); 16 | if (now == k) return data[now]; 17 | int next = now - 1; 18 | if (0 <= next && next < MAX && !data[next]) { 19 | data[next] = data[now] + 1; 20 | q.push(next); 21 | } 22 | next = now + 1; 23 | if (0 <= next && next < MAX && !data[next]) { 24 | data[next] = data[now] + 1; 25 | q.push(next); 26 | } 27 | next = now * 2; 28 | if (0 <= next && next < MAX && !data[next]) { 29 | data[next] = data[now] + 1; 30 | q.push(next); 31 | } 32 | } 33 | } 34 | 35 | int main() { 36 | cin >> n >> k; 37 | cout << bfs(); 38 | } 39 | -------------------------------------------------------------------------------- /Solutions/[14]_2.java: -------------------------------------------------------------------------------- 1 | import java.util.LinkedList; 2 | import java.util.Queue; 3 | import java.util.Scanner; 4 | 5 | public class Main { 6 | 7 | public static int MAX = 100001; 8 | public static int n, k; 9 | public static int[] data = new int[MAX]; 10 | 11 | // 너비 우선 탐색(BFS) 12 | public static int bfs() { 13 | Queue q = new LinkedList(); 14 | q.offer(n); 15 | while (!q.isEmpty()) { 16 | int now = q.poll(); 17 | if (now == k) return data[now]; 18 | int next = now - 1; 19 | if (0 <= next && next < MAX && data[next] == 0) { 20 | data[next] = data[now] + 1; 21 | q.add(next); 22 | } 23 | next = now + 1; 24 | if (0 <= next && next < MAX && data[next] == 0) { 25 | data[next] = data[now] + 1; 26 | q.add(next); 27 | } 28 | next = now * 2; 29 | if (0 <= next && next < MAX && data[next] == 0) { 30 | data[next] = data[now] + 1; 31 | q.add(next); 32 | } 33 | } 34 | return -1; 35 | } 36 | 37 | public static void main(String[] args) { 38 | Scanner sc = new Scanner(System.in); 39 | 40 | n = sc.nextInt(); 41 | k = sc.nextInt(); 42 | 43 | System.out.println(bfs()); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /Solutions/[14]_2.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | 3 | MAX = 100001 4 | n, k = map(int, input().split()) 5 | array = [0] * MAX 6 | 7 | def bfs(): 8 | q = deque([n]) 9 | while q: 10 | now_pos = q.popleft() 11 | if now_pos == k: 12 | return array[now_pos] 13 | for next_pos in (now_pos - 1, now_pos + 1, now_pos * 2): 14 | if 0 <= next_pos < MAX and not array[next_pos]: 15 | array[next_pos] = array[now_pos] + 1 16 | q.append(next_pos) 17 | 18 | print(bfs()) -------------------------------------------------------------------------------- /Solutions/[15]_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int n, m, result; 6 | vector adj[101]; 7 | bool visited[101]; 8 | 9 | void dfs(int x) { 10 | result++; 11 | visited[x] = true; 12 | for (int i = 0; i < adj[x].size(); i++) { 13 | int y = adj[x][i]; 14 | if (!visited[y]) { 15 | dfs(y); 16 | } 17 | } 18 | } 19 | 20 | int main() { 21 | cin >> n >> m; 22 | for (int i = 0; i < m; i++) { 23 | int x, y; 24 | cin >> x >> y; 25 | adj[x].push_back(y); 26 | adj[y].push_back(x); 27 | } 28 | dfs(1); 29 | cout << result - 1; 30 | } 31 | -------------------------------------------------------------------------------- /Solutions/[15]_1.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Scanner; 3 | 4 | public class Main { 5 | 6 | public static int n, m, result; 7 | public static ArrayList[] adj = new ArrayList[101]; 8 | public static boolean[] visited = new boolean[101]; 9 | 10 | // 깊이 우선 탐색(DFS) 11 | public static void dfs(int x) { 12 | result++; 13 | visited[x] = true; 14 | for (int i = 0; i < adj[x].size(); i++) { 15 | int y = adj[x].get(i); 16 | if (!visited[y]) { 17 | dfs(y); 18 | } 19 | } 20 | } 21 | 22 | public static void main(String[] args) { 23 | Scanner sc = new Scanner(System.in); 24 | 25 | n = sc.nextInt(); 26 | m = sc.nextInt(); 27 | 28 | for (int i = 1; i <= n; i++) { 29 | adj[i] = new ArrayList(); 30 | } 31 | 32 | for (int i = 0; i < m; i++) { 33 | int x = sc.nextInt(); 34 | int y = sc.nextInt(); 35 | adj[x].add(y); 36 | adj[y].add(x); 37 | } 38 | 39 | dfs(1); 40 | System.out.println(result - 1); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /Solutions/[15]_1.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | m = int(input()) 3 | 4 | adj = [[] for _ in range(n + 1)] 5 | visited = [False] * (n + 1) 6 | count = 0 7 | 8 | for _ in range(m): 9 | x, y = map(int, input().split()) 10 | adj[x].append(y) 11 | adj[y].append(x) 12 | 13 | def dfs(now_pos): 14 | global count 15 | count += 1 16 | visited[now_pos] = True 17 | for next_pos in adj[now_pos]: 18 | if not visited[next_pos]: 19 | dfs(next_pos) 20 | 21 | dfs(1) 22 | print(count - 1) -------------------------------------------------------------------------------- /Solutions/[15]_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int t, m, n, k; 6 | int data[50][50]; 7 | bool visited[50][50]; 8 | // (상, 하, 좌, 우) 4가지 방향 9 | int dx[] = {-1, 1, 0, 0}; 10 | int dy[] = {0, 0, -1, 1}; 11 | 12 | void dfs(int x, int y) { 13 | visited[x][y] = true; 14 | for (int i = 0; i < 4; i++) { 15 | int nx = x + dx[i]; 16 | int ny = y + dy[i]; 17 | if (nx < 0 || nx >= n || ny < 0 || ny >= m) { 18 | continue; 19 | } 20 | if (data[nx][ny] && !visited[nx][ny]) { 21 | dfs(nx, ny); 22 | } 23 | } 24 | } 25 | 26 | int main() { 27 | cin >> t; 28 | while (t--) { 29 | cin >> m >> n >> k; 30 | memset(data, 0, sizeof(data)); 31 | memset(visited, false, sizeof(visited)); 32 | 33 | for (int i = 0; i < k; i++) { 34 | int y, x; 35 | cin >> y >> x; 36 | data[x][y] = 1; 37 | } 38 | 39 | int result = 0; 40 | for (int i = 0; i < n; i++) { 41 | for (int j = 0; j < m; j++) { 42 | if (data[i][j] && !visited[i][j]) { 43 | dfs(i, j); 44 | result++; 45 | } 46 | } 47 | } 48 | 49 | cout << result << '\n'; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Solutions/[15]_2.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | 5 | public static int t, m, n, k; 6 | public static int[][] data = new int[50][50]; 7 | public static boolean[][] visited = new boolean[50][50]; 8 | public static int[] dx = {-1, 1, 0, 0}; 9 | public static int[] dy = {0, 0, -1, 1}; 10 | 11 | // 깊이 우선 탐색(DFS) 12 | public static void dfs(int x, int y) { 13 | visited[x][y] = true; 14 | for (int i = 0; i < 4; i++) { 15 | int nx = x + dx[i]; 16 | int ny = y + dy[i]; 17 | if (nx < 0 || nx >= n || ny < 0 || ny >= m) { 18 | continue; 19 | } 20 | if (data[nx][ny] > 0 && !visited[nx][ny]) { 21 | dfs(nx, ny); 22 | } 23 | } 24 | } 25 | 26 | public static void main(String[] args) { 27 | Scanner sc = new Scanner(System.in); 28 | 29 | t = sc.nextInt(); 30 | while (t-- > 0) { 31 | m = sc.nextInt(); 32 | n = sc.nextInt(); 33 | k = sc.nextInt(); 34 | data = new int[50][50]; 35 | visited = new boolean[50][50]; 36 | 37 | for (int i = 0; i < k; i++) { 38 | int y = sc.nextInt(); 39 | int x = sc.nextInt(); 40 | data[x][y] = 1; 41 | } 42 | 43 | int result = 0; 44 | for (int i = 0; i < n; i++) { 45 | for (int j = 0; j < m; j++) { 46 | if (data[i][j] > 0 && !visited[i][j]) { 47 | dfs(i, j); 48 | result++; 49 | } 50 | } 51 | } 52 | 53 | System.out.println(result); 54 | } 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /Solutions/[15]_2.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.setrecursionlimit(100000) 3 | 4 | def dfs(x, y): 5 | visited[x][y] = True 6 | directions = [(-1, 0), (1, 0), (0, -1), (0, 1)] 7 | 8 | for dx, dy in directions: 9 | nx, ny = x + dx, y + dy 10 | if nx < 0 or nx >= n or ny < 0 or ny >= m: 11 | continue 12 | if array[nx][ny] and not visited[nx][ny]: 13 | dfs(nx, ny) 14 | 15 | for _ in range(int(input())): 16 | m, n, k = map(int, input().split()) 17 | 18 | array = [[0] * m for _ in range(n)] 19 | visited = [[False] * m for _ in range(n)] 20 | 21 | for _ in range(k): 22 | y, x = map(int, input().split()) 23 | array[x][y] = 1 24 | 25 | result = 0 26 | for i in range(n): 27 | for j in range(m): 28 | if array[i][j] and not visited[i][j]: 29 | dfs(i, j) 30 | result += 1 31 | 32 | print(result) -------------------------------------------------------------------------------- /Solutions/[15]_3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int n, m, max_value; 6 | vector adj[10001]; 7 | bool visited[10001]; 8 | vector result; 9 | 10 | int bfs(int x) { 11 | queue q; 12 | q.push(x); 13 | memset(visited, false, sizeof(visited)); 14 | visited[x] = true; 15 | int cnt = 1; 16 | while (!q.empty()) { 17 | int now = q.front(); 18 | q.pop(); 19 | for (int i = 0; i < adj[now].size(); i++) { 20 | int y = adj[now][i]; 21 | if (!visited[y]) { 22 | q.push(y); 23 | visited[y] = true; 24 | cnt++; 25 | } 26 | } 27 | } 28 | return cnt; 29 | } 30 | 31 | int main() { 32 | cin >> n >> m; 33 | for (int i = 0; i < m; i++) { 34 | int x, y; 35 | cin >> x >> y; 36 | adj[y].push_back(x); 37 | } 38 | for (int i = 1; i <= n; i++) { 39 | int cnt = bfs(i); 40 | if (cnt > max_value) { 41 | result.clear(); 42 | result.push_back(i); 43 | max_value = cnt; 44 | } 45 | else if (cnt == max_value) { 46 | result.push_back(i); 47 | } 48 | } 49 | for (int i = 0; i < result.size(); i++) { 50 | cout << result[i] << ' '; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Solutions/[15]_3.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.InputStreamReader; 3 | import java.util.ArrayList; 4 | import java.util.LinkedList; 5 | import java.util.Queue; 6 | import java.util.StringTokenizer; 7 | 8 | public class Main { 9 | 10 | public static int n, m, max_value; 11 | public static ArrayList[] adj = new ArrayList[10001]; 12 | public static boolean[] visited = new boolean[10001]; 13 | public static int[] saved = new int[10001]; 14 | public static StringBuffer result = new StringBuffer(); 15 | 16 | public static void bfs(int x) { 17 | Queue q = new LinkedList(); 18 | q.add(x); 19 | visited = new boolean[10001]; 20 | visited[x] = true; 21 | 22 | while (!q.isEmpty()) { 23 | int now = q.poll(); 24 | for (int i = 0; i < adj[now].size(); i++) { 25 | int y = adj[now].get(i); 26 | if (!visited[y]) { 27 | q.add(y); 28 | visited[y] = true; 29 | // 현재 노드(now)에 도달할 수 있는 노드들의 값을 증가 30 | saved[y]++; 31 | } 32 | } 33 | } 34 | } 35 | 36 | public static void main(String[] args) throws Exception { 37 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 38 | 39 | StringTokenizer st = new StringTokenizer(br.readLine()); 40 | n = Integer.parseInt(st.nextToken()); 41 | m = Integer.parseInt(st.nextToken()); 42 | 43 | for (int i = 1; i <= n; i++) { 44 | adj[i] = new ArrayList(); 45 | } 46 | 47 | for (int i = 0; i < m; i++) { 48 | st = new StringTokenizer(br.readLine()); 49 | int x = Integer.parseInt(st.nextToken()); 50 | int y = Integer.parseInt(st.nextToken()); 51 | adj[x].add(y); // (도착 노드, 출발 노드) 순으로 삽입 52 | } 53 | 54 | for (int i = 1; i <= n; i++) { 55 | bfs(i); 56 | } 57 | 58 | for (int i = 1; i <= n; i++) { 59 | max_value = Math.max(max_value, saved[i]); 60 | } 61 | 62 | for (int i = 1; i <= n; i++) { 63 | if (saved[i] == max_value) { 64 | result.append(i + " "); 65 | } 66 | } 67 | 68 | System.out.println(result); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /Solutions/[15]_3.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | 3 | n, m = map(int, input().split()) 4 | adj = [[] for _ in range(n + 1)] 5 | 6 | for _ in range(m): 7 | x, y = map(int, input().split()) 8 | adj[y].append(x) 9 | 10 | def bfs(v): 11 | q = deque([v]) 12 | visited = [False] * (n + 1) 13 | visited[v] = True 14 | count = 1 15 | while q: 16 | v = q.popleft() 17 | for e in adj[v]: 18 | if not visited[e]: 19 | q.append(e) 20 | visited[e] = True 21 | count += 1 22 | return count 23 | 24 | result = [] 25 | max_value = -1 26 | 27 | for i in range(1, n + 1): 28 | c = bfs(i) 29 | if c > max_value: 30 | result = [i] 31 | max_value = c 32 | elif c == max_value: 33 | result.append(i) 34 | max_value = c 35 | 36 | for e in result: 37 | print(e, end=" ") -------------------------------------------------------------------------------- /Solutions/[16]_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int t, n, m, start; 6 | vector > adj[10001]; 7 | int d[10001]; 8 | 9 | void dijkstra() { 10 | priority_queue > pq; 11 | pq.push(make_pair(0, start)); 12 | d[start] = 0; 13 | while (!pq.empty()) { 14 | int dist = -pq.top().first; // 현재 노드까지의 비용 15 | int now = pq.top().second; // 현재 노드 16 | pq.pop(); 17 | if (d[now] < dist) continue; 18 | for (int i = 0; i < adj[now].size(); i++) { 19 | int next = adj[now][i].first; // 다음 노드 20 | int nextDist = dist + adj[now][i].second; // 현재 노드를 거쳐 가는 비용 21 | if (d[next] > nextDist) { 22 | d[next] = nextDist; 23 | pq.push(make_pair(-nextDist, next)); 24 | } 25 | } 26 | } 27 | } 28 | 29 | int main() { 30 | cin >> t; 31 | while (t--) { 32 | cin >> n >> m >> start; 33 | for (int i = 1; i <= n; i++) { 34 | adj[i].clear(); 35 | } 36 | // 각 거리를 1e9로 초기화하기 위하여 fill() 함수 호출 37 | fill(d, d + 10001, 1e9); 38 | for (int i = 0; i < m; i++) { 39 | int x, y, cost; 40 | cin >> x >> y >> cost; 41 | adj[y].push_back(make_pair(x, cost)); 42 | } 43 | dijkstra(); 44 | int cnt = 0; 45 | int maxDistance = 0; 46 | for (int i = 1; i<= n; i++) { 47 | if (d[i] != 1e9) { 48 | cnt++; 49 | maxDistance = max(maxDistance, d[i]); 50 | } 51 | } 52 | cout << cnt << ' ' << maxDistance << '\n'; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Solutions/[16]_1.py: -------------------------------------------------------------------------------- 1 | import heapq 2 | import sys 3 | input = sys.stdin.readline 4 | 5 | def dijkstra(start): 6 | heap_data = [] 7 | heapq.heappush(heap_data, (0, start)) 8 | distance[start] = 0 9 | 10 | while heap_data: 11 | dist, now = heapq.heappop(heap_data) 12 | if distance[now] < dist: 13 | continue 14 | for i in adj[now]: 15 | cost = dist + i[1] 16 | if distance[i[0]] > cost: 17 | distance[i[0]] = cost 18 | heapq.heappush(heap_data, (cost, i[0])) 19 | 20 | for _ in range(int(input())): 21 | n, m, start = map(int, input().split()) 22 | adj = [[] for i in range(n + 1)] 23 | distance = [1e9] * (n + 1) 24 | 25 | for _ in range(m): 26 | x, y, cost = map(int, input().split()) 27 | adj[y].append((x, cost)) 28 | 29 | dijkstra(start) 30 | 31 | count = 0 32 | max_distance = 0 33 | for i in distance: 34 | if i != 1e9: 35 | count += 1 36 | if i > max_distance: 37 | max_distance = i 38 | 39 | print(count, max_distance) -------------------------------------------------------------------------------- /Solutions/[16]_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int n, m, start, finish; 6 | vector > adj[500]; 7 | int d[500]; 8 | vector > reverseAdj[500]; 9 | bool dropped[500][500]; 10 | 11 | void dijkstra() { 12 | priority_queue > pq; 13 | pq.push(make_pair(0, start)); 14 | d[start] = 0; 15 | while (!pq.empty()) { 16 | int dist = -pq.top().first; // 현재 노드까지의 비용 17 | int now = pq.top().second; // 현재 노드 18 | pq.pop(); 19 | if (d[now] < dist) continue; 20 | for (int i = 0; i < adj[now].size(); i++) { 21 | int next = adj[now][i].first; // 다음 노드 22 | int nextDist = dist + adj[now][i].second; // 현재 노드를 거쳐 가는 비용 23 | if (d[next] > nextDist && !dropped[now][next]) { 24 | d[next] = nextDist; 25 | pq.push(make_pair(-nextDist, next)); 26 | } 27 | } 28 | } 29 | } 30 | 31 | void bfs() { 32 | queue q; 33 | q.push(finish); 34 | while (!q.empty()) { 35 | int now = q.front(); 36 | q.pop(); 37 | if (now == start) continue; 38 | for (int i = 0; i < reverseAdj[now].size(); i++) { 39 | int prev = reverseAdj[now][i].first; 40 | int cost = reverseAdj[now][i].second; 41 | // 최단 경로에 포함된 경우 체크하기 42 | if (d[now] == d[prev] + cost) { 43 | dropped[prev][now] = true; 44 | q.push(prev); 45 | } 46 | } 47 | } 48 | } 49 | 50 | int main() { 51 | while (true) { 52 | cin >> n >> m; 53 | if (n == 0) break; 54 | cin >> start >> finish; 55 | for (int i = 0; i < n; i++) { 56 | adj[i].clear(); 57 | reverseAdj[i].clear(); 58 | } 59 | for (int i = 0; i < m; i++) { 60 | int x, y, cost; 61 | cin >> x >> y >> cost; 62 | adj[x].push_back(make_pair(y, cost)); 63 | reverseAdj[y].push_back(make_pair(x, cost)); 64 | } 65 | // false로 초기화할 때는 memset() 함수 이용 66 | memset(dropped, false, sizeof(dropped)); 67 | // 특정한 값으로 초기화할 때는 fill() 함수 이용 68 | fill(d, d + 500, 1e9); 69 | dijkstra(); 70 | bfs(); 71 | fill(d, d + 500, 1e9); 72 | dijkstra(); 73 | if (d[finish] != 1e9) { 74 | cout << d[finish] << '\n'; 75 | } 76 | else { 77 | cout << -1 << '\n'; 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /Solutions/[16]_2.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | import heapq 3 | import sys 4 | input = sys.stdin.readline 5 | 6 | def dijkstra(): 7 | heap_data = [] 8 | heapq.heappush(heap_data, (0, start)) 9 | distance[start] = 0 10 | while heap_data: 11 | dist, now = heapq.heappop(heap_data) 12 | if distance[now] < dist: 13 | continue 14 | for i in adj[now]: 15 | cost = dist + i[1] 16 | if distance[i[0]] > cost and not dropped[now][i[0]]: 17 | distance[i[0]] = cost 18 | heapq.heappush(heap_data, (cost, i[0])) 19 | 20 | def bfs(): 21 | q = deque() 22 | q.append(end) 23 | while q: 24 | now = q.popleft() 25 | if now == start: 26 | continue 27 | for prev, cost in reverse_adj[now]: 28 | if distance[now] == distance[prev] + cost: 29 | dropped[prev][now] = True 30 | q.append(prev) 31 | 32 | while True: 33 | n, m = map(int, input().split()) 34 | if n == 0: 35 | break 36 | start, end = map(int, input().split()) 37 | 38 | adj = [[] for _ in range(n + 1)] 39 | reverse_adj = [[] for _ in range(n + 1)] 40 | 41 | for _ in range(m): 42 | x, y, cost = map(int, input().split()) 43 | adj[x].append((y, cost)) 44 | reverse_adj[y].append((x, cost)) 45 | 46 | dropped = [[False] * (n + 1) for _ in range(n + 1)] 47 | distance= [1e9] * (n + 1) 48 | dijkstra() 49 | bfs() 50 | distance= [1e9] * (n + 1) 51 | dijkstra() 52 | 53 | if distance[end] != 1e9: 54 | print(distance[end]) 55 | else: 56 | print(-1) -------------------------------------------------------------------------------- /Solutions/[16]_3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int n, m; 6 | int parent[1001]; 7 | vector > locations; 8 | vector > > edges; 9 | 10 | // 두 점 사이의 거리를 계산하는 함수 11 | double getDistance(pair p1, pair p2) { 12 | long long a = p1.first - p2.first; 13 | long long b = p1.second - p2.second; 14 | return sqrt((a * a) + (b * b)); 15 | } 16 | 17 | int getParent(int a) { 18 | if (a == parent[a]) return a; 19 | return parent[a] = getParent(parent[a]); 20 | } 21 | 22 | void unionParent(int a, int b) { 23 | a = getParent(a); 24 | b = getParent(b); 25 | if (a < b) parent[b] = a; 26 | else parent[a] = b; 27 | } 28 | 29 | int compareParent(int a, int b) { 30 | a = getParent(a); 31 | b = getParent(b); 32 | if (a == b) return true; 33 | return false; 34 | } 35 | 36 | int main() { 37 | cin >> n >> m; 38 | for (int i = 0; i < n; i++) { 39 | int x, y; 40 | cin >> x >> y; 41 | locations.push_back(make_pair(x, y)); 42 | } 43 | 44 | // 모든 점 사이의 거리를 계산하여 간선(Edge) 리스트에 저장 45 | for (int i = 0; i < n - 1; i++) { 46 | for (int j = i + 1; j < n; j++) { 47 | edges.push_back(make_pair(getDistance(locations[i], locations[j]), make_pair(i + 1, j + 1))); 48 | } 49 | } 50 | 51 | for (int i = 1; i <= n; i++) { 52 | parent[i] = i; 53 | } 54 | 55 | for (int i = 0; i < m; i++) { 56 | int a, b; 57 | cin >> a >> b; 58 | unionParent(a, b); 59 | } 60 | 61 | // 간선의 길이를 기준으로 오름차순 정렬 62 | sort(edges.begin(), edges.end()); 63 | 64 | double result = 0; 65 | for (int i = 0; i < edges.size(); i++) { 66 | double cost = edges[i].first; 67 | int a = edges[i].second.first; 68 | int b = edges[i].second.second; 69 | if (!compareParent(a, b)) { 70 | unionParent(a, b); 71 | result += cost; 72 | } 73 | } 74 | 75 | printf("%0.2f", result); 76 | } 77 | -------------------------------------------------------------------------------- /Solutions/[16]_3.py: -------------------------------------------------------------------------------- 1 | import math 2 | import sys 3 | input = sys.stdin.readline 4 | 5 | def get_distance(p1, p2): 6 | a = p1[0] - p2[0] 7 | b = p1[1] - p2[1] 8 | return math.sqrt((a * a) + (b * b)) 9 | 10 | def get_parent(parent, n): 11 | if parent[n] == n: 12 | return n 13 | return get_parent(parent, parent[n]) 14 | 15 | def union_parent(parent, a, b): 16 | a = get_parent(parent, a) 17 | b = get_parent(parent, b) 18 | if a < b: 19 | parent[b] = a 20 | else: 21 | parent[a] = b 22 | 23 | def find_parent(parent, a, b): 24 | a = get_parent(parent, a) 25 | b = get_parent(parent, b) 26 | if a == b: 27 | return True 28 | else: 29 | return False 30 | 31 | edges = [] 32 | parent = {} 33 | locations = [] 34 | n, m = map(int, input().split()) 35 | 36 | for _ in range(n): 37 | x, y = map(int, input().split()) 38 | locations.append((x, y)) 39 | 40 | length = len(locations) 41 | for i in range(length - 1): 42 | for j in range(i + 1, length): 43 | edges.append((i + 1, j + 1, get_distance(locations[i], locations[j]))) 44 | 45 | for i in range(1, n + 1): 46 | parent[i] = i 47 | 48 | for i in range(m): 49 | a, b = map(int, input().split()) 50 | union_parent(parent, a, b) 51 | 52 | edges.sort(key=lambda data: data[2]) 53 | 54 | result = 0 55 | for a, b, cost in edges: 56 | if not find_parent(parent, a, b): 57 | union_parent(parent, a, b) 58 | result += cost 59 | 60 | print("%0.2f" % result) -------------------------------------------------------------------------------- /Solutions/[17]_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int data[6] = {500, 100, 50, 10, 5, 1}; 6 | int changes, result; 7 | 8 | int main(void) { 9 | cin >> changes; 10 | changes = 1000 - changes; 11 | for (int i = 0; i < 6; i++) { 12 | result += changes / data[i]; 13 | changes %= data[i]; 14 | } 15 | cout << result; 16 | } 17 | -------------------------------------------------------------------------------- /Solutions/[17]_1.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | Scanner sc = new Scanner(System.in); 7 | 8 | int[] data = {500, 100, 50, 10, 5, 1}; 9 | int changes, result = 0; 10 | 11 | changes = sc.nextInt(); 12 | changes = 1000 - changes; 13 | for (int i = 0; i < 6; i++) { 14 | result += changes / data[i]; 15 | changes %= data[i]; 16 | } 17 | 18 | System.out.println(result); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Solutions/[17]_1.py: -------------------------------------------------------------------------------- 1 | changes = 1000 - int(input()) 2 | count = 0 3 | 4 | for i in [500, 100, 50, 10, 5, 1]: 5 | count += changes // i 6 | changes %= i 7 | 8 | print(count) -------------------------------------------------------------------------------- /Solutions/[17]_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | string data; 6 | int count0 = 0; // 전부 0으로 바꾸는 경우 7 | int count1 = 0; // 전부 1로 바꾸는 경우 8 | 9 | int main(void) { 10 | cin >> data; 11 | if (data[0] == '1') count0 += 1; 12 | else count1 += 1; 13 | 14 | for (int i = 0; i < data.length() - 1; i++) { 15 | if (data[i] != data[i + 1]) { 16 | // 다음 수에서 1로 바뀌는 경우 17 | if (data[i + 1] == '1') count0 += 1; 18 | else count1 += 1; 19 | } 20 | } 21 | cout << min(count0, count1); 22 | } 23 | -------------------------------------------------------------------------------- /Solutions/[17]_2.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | Scanner sc = new Scanner(System.in); 7 | 8 | int count0 = 0; // 전부 0으로 바꾸는 경우 9 | int count1 = 0; // 전부 1로 바꾸는 경우 10 | String data = sc.nextLine(); 11 | 12 | if (data.charAt(0) == '1') count0 += 1; 13 | else count1 += 1; 14 | 15 | for (int i = 0; i < data.length() - 1; i++) { 16 | if (data.charAt(i) != data.charAt(i + 1)) { 17 | // 다음 수에서 1로 바뀌는 경우 18 | if (data.charAt(i + 1) == '1') count0 += 1; 19 | else count1 += 1; 20 | } 21 | } 22 | 23 | System.out.println(Math.min(count0, count1)); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Solutions/[17]_2.py: -------------------------------------------------------------------------------- 1 | data = input() 2 | count0 = 0 # 전부 0으로 바꾸는 경우 3 | count1 = 0 # 전부 1로 바꾸는 경우 4 | 5 | if data[0] == '1': 6 | count0 += 1 7 | else: 8 | count1 += 1 9 | 10 | for i in range(len(data) - 1): 11 | if data[i] != data[i + 1]: 12 | # 다음 수에서 1로 바뀌는 경우 13 | if data[i + 1] == '1': 14 | count0 += 1 15 | # 다음 수에서 0으로 바뀌는 경우 16 | else: 17 | count1 += 1 18 | 19 | print(min(count0, count1)) -------------------------------------------------------------------------------- /Solutions/[17]_3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int n; 6 | vector data; 7 | 8 | int main(void) { 9 | cin >> n; 10 | for (int i = 0; i < n; i++) { 11 | int x; 12 | cin >> x; 13 | data.push_back(x); 14 | } 15 | 16 | // 오름차순 정렬 수행 17 | sort(data.begin(), data.end()); 18 | 19 | // 불만도의 합 계산 20 | long long result = 0; 21 | for (int i = 1; i <= n; i++) { 22 | result += abs(i - data[i - 1]); 23 | } 24 | 25 | cout << result; 26 | } 27 | -------------------------------------------------------------------------------- /Solutions/[17]_3.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Collections; 3 | import java.util.Scanner; 4 | 5 | public class Main { 6 | 7 | public static void main(String[] args) { 8 | Scanner sc = new Scanner(System.in); 9 | 10 | int n = sc.nextInt(); 11 | ArrayList data = new ArrayList(); 12 | 13 | for (int i = 0; i < n; i++) { 14 | data.add(sc.nextInt()); 15 | } 16 | 17 | // 오름차순 정렬 수행 18 | Collections.sort(data); 19 | 20 | // 불만도의 합 계산 21 | long result = 0; 22 | for (int i = 1; i <= n; i++) { 23 | result += Math.abs(i - data.get(i - 1)); 24 | } 25 | 26 | System.out.println(result); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Solutions/[17]_3.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | array = [] 3 | 4 | for _ in range(n): 5 | array.append(int(input())) 6 | 7 | # 오름차순 정렬 수행 8 | array.sort() 9 | 10 | # 불만도의 합 계산 11 | result = 0 12 | for i in range(1, len(array) + 1): 13 | result += abs(i - array[i - 1]) 14 | 15 | print(result) -------------------------------------------------------------------------------- /Solutions/[17]_4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int n, m, result, cnt; 6 | vector cranes; 7 | vector boxes; 8 | 9 | int positions[50]; // 각 크레인이 현재 옮겨야 하는 박스의 번호 (0부터 시작) 10 | bool checked[10000]; // 각 박스를 옮겼는지의 여부 11 | 12 | int main(void) { 13 | cin >> n; 14 | for (int i = 0; i < n; i++) { 15 | int x; 16 | cin >> x; 17 | cranes.push_back(x); 18 | } 19 | 20 | cin >> m; 21 | for (int i = 0; i < m; i++) { 22 | int x; 23 | cin >> x; 24 | boxes.push_back(x); 25 | } 26 | 27 | // 최적의 해를 구해야 하므로, 내림차순 정렬 28 | sort(cranes.begin(), cranes.end(), greater()); 29 | sort(boxes.begin(), boxes.end(), greater()); 30 | 31 | // 모든 박스를 옮길 수 없는 경우 32 | if (cranes[0] < boxes[0]) { 33 | cout << -1; 34 | return 0; 35 | } 36 | 37 | while (true) { 38 | if (cnt == m) break; 39 | // 모든 크레인에 대하여 각각 처리 40 | for (int i = 0; i < n; i++) { 41 | while (positions[i] < m) { 42 | // 아직 안 옮긴 박스 중에서, 옮길 수 있는 박스를 만날 때까지 반복 43 | if (!checked[positions[i]] && cranes[i] >= boxes[positions[i]]) { 44 | checked[positions[i]] = true; 45 | positions[i] += 1; 46 | cnt += 1; 47 | break; 48 | } 49 | positions[i] += 1; 50 | } 51 | } 52 | result += 1; 53 | } 54 | 55 | cout << result; 56 | } 57 | -------------------------------------------------------------------------------- /Solutions/[17]_4.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Collections; 3 | import java.util.Scanner; 4 | 5 | public class Main { 6 | 7 | public static void main(String[] args) { 8 | Scanner sc = new Scanner(System.in); 9 | 10 | int n, m, result = 0, cnt = 0; 11 | ArrayList cranes = new ArrayList(); 12 | ArrayList boxes = new ArrayList(); 13 | 14 | int[] positions = new int[50]; // 각 크레인이 현재 옮겨야 하는 박스의 번호 (0부터 시작) 15 | boolean[] checked = new boolean[10000]; // 각 박스를 옮겼는지의 여부 16 | 17 | n = sc.nextInt(); 18 | for (int i = 0; i < n; i++) { 19 | cranes.add(sc.nextInt()); 20 | } 21 | 22 | m = sc.nextInt(); 23 | for (int i = 0; i < m; i++) { 24 | boxes.add(sc.nextInt()); 25 | } 26 | 27 | // 최적의 해를 구해야 하므로, 내림차순 정렬 28 | Collections.sort(cranes); 29 | Collections.sort(boxes); 30 | Collections.reverse(cranes); 31 | Collections.reverse(boxes); 32 | 33 | 34 | // 모든 박스를 옮길 수 없는 경우 35 | if (cranes.get(0) < boxes.get(0)) { 36 | System.out.println(-1); 37 | return; 38 | } 39 | 40 | while (true) { 41 | if (cnt == m) break; 42 | // 모든 크레인에 대하여 각각 처리 43 | for (int i = 0; i < n; i++) { 44 | while (positions[i] < m) { 45 | // 아직 안 옮긴 박스 중에서, 옮길 수 있는 박스를 만날 때까지 반복 46 | if (!checked[positions[i]] && cranes.get(i) >= boxes.get(positions[i])) { 47 | checked[positions[i]] = true; 48 | positions[i] += 1; 49 | cnt += 1; 50 | break; 51 | } 52 | positions[i] += 1; 53 | } 54 | } 55 | result += 1; 56 | } 57 | 58 | System.out.println(result); 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /Solutions/[17]_4.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | n = int(input()) 4 | cranes = list(map(int, input().split())) 5 | 6 | m = int(input()) 7 | boxes = list(map(int, input().split())) 8 | 9 | # 모든 박스를 옮길 수 없는 경우 10 | if max(cranes) < max(boxes): 11 | print(-1) 12 | sys.exit() 13 | 14 | # 각 크레인이 현재 옮겨야 하는 박스의 번호 (0부터 시작) 15 | positions = [0] * n 16 | # 각 박스를 옮겼는지의 여부 17 | checked = [False] * m 18 | # 최적의 해를 구해야 하므로, 내림차순 정렬 19 | cranes.sort(reverse=True) 20 | boxes.sort(reverse=True) 21 | 22 | result = 0 23 | count = 0 24 | 25 | while True: 26 | if count == len(boxes): # 박스를 다 옮겼다면 종료 27 | break 28 | for i in range(n): # 모든 크레인에 대하여 각각 처리 29 | while positions[i] < len(boxes): 30 | # 아직 안 옮긴 박스 중에서, 옮길 수 있는 박스를 만날 때까지 반복 31 | if not checked[positions[i]] and cranes[i] >= boxes[positions[i]]: 32 | checked[positions[i]] = True 33 | positions[i] += 1 34 | count += 1 35 | break 36 | positions[i] += 1 37 | result += 1 38 | 39 | print(result) -------------------------------------------------------------------------------- /Solutions/[18]_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int n, k, result; 6 | vector data; 7 | vector distances; 8 | 9 | int main(void) { 10 | cin >> n >> k; 11 | 12 | // 집중국의 개수가 n 이상일 때 13 | if (k >= n) { 14 | cout << 0; // 각 센서의 위치에 설치하면 되므로 정답은 0 15 | return 0; 16 | } 17 | 18 | // 모든 센서의 위치를 입력 받아 오름차순 정렬 19 | for (int i = 0; i < n; i++) { 20 | int x; 21 | cin >> x; 22 | data.push_back(x); 23 | } 24 | sort(data.begin(), data.end()); 25 | 26 | // 각 센서 간의 거리를 계산하며 기록 27 | for (int i = 1; i < n; i++) { 28 | distances.push_back(data[i] - data[i - 1]); 29 | result += data[i] - data[i - 1]; 30 | } 31 | 32 | // 내림차순 정렬 33 | sort(distances.begin(), distances.end(), greater()); 34 | 35 | // 가장 긴 거리부터 하나씩 제거 36 | for (int i = 0; i < k - 1; i++) { 37 | result -= distances[i]; 38 | } 39 | 40 | cout << result; 41 | } 42 | -------------------------------------------------------------------------------- /Solutions/[18]_1.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Collections; 3 | import java.util.Scanner; 4 | 5 | public class Main { 6 | 7 | public static void main(String[] args) { 8 | Scanner sc = new Scanner(System.in); 9 | 10 | int n = sc.nextInt(); 11 | int k = sc.nextInt(); 12 | 13 | ArrayList data = new ArrayList(); 14 | ArrayList distances = new ArrayList(); 15 | 16 | // 집중국의 개수가 n 이상일 때 17 | if (k >= n) { 18 | System.out.println(0); // 각 센서의 위치에 설치하면 되므로 정답은 0 19 | return; 20 | } 21 | 22 | // 모든 센서의 위치를 입력 받아 오름차순 정렬 23 | for (int i = 0; i < n; i++) { 24 | int x = sc.nextInt(); 25 | data.add(x); 26 | } 27 | Collections.sort(data); 28 | 29 | int result = 0; 30 | 31 | // 각 센서 간의 거리를 계산하며 기록 32 | for (int i = 1; i < n; i++) { 33 | distances.add(data.get(i) - data.get(i - 1)); 34 | result += data.get(i) - data.get(i - 1); 35 | } 36 | 37 | // 내림차순 정렬 38 | Collections.sort(distances); 39 | Collections.reverse(distances); 40 | 41 | // 가장 긴 거리부터 하나씩 제거 42 | for (int i = 0; i < k - 1; i++) { 43 | result -= distances.get(i); 44 | } 45 | 46 | System.out.println(result); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /Solutions/[18]_1.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | n = int(input()) 4 | k = int(input()) 5 | 6 | # 집중국의 개수가 n 이상일 때 7 | if k >= n: 8 | print(0) # 각 센서의 위치에 설치하면 되므로 정답은 0 9 | sys.exit() 10 | 11 | # 모든 센서의 위치를 입력 받아 오름차순 정렬 12 | array = list(map(int, input().split(' '))) 13 | array.sort() 14 | 15 | # 각 센서 간의 거리를 계산하여 내림차순 정렬 16 | distances = [] 17 | for i in range(1, n): 18 | distances.append(array[i] - array[i - 1]) 19 | distances.sort(reverse=True) 20 | 21 | # 가장 긴 거리부터 하나씩 제거 22 | for i in range(k - 1): 23 | distances[i] = 0 24 | 25 | print(sum(distances)) -------------------------------------------------------------------------------- /Solutions/[18]_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int n, m, result; 6 | vector data; 7 | // C++에서는 기본적으로 우선순위 큐가 최대 힙(Max Heap) 8 | priority_queue positive, negative; 9 | int max_value = -10001; 10 | int min_value = 10001; 11 | 12 | int main(void) { 13 | cin >> n >> m; 14 | 15 | for (int i = 0; i < n; i++) { 16 | int x; 17 | cin >> x; 18 | data.push_back(x); 19 | max_value = max(max_value, x); 20 | min_value = min(min_value, x); 21 | } 22 | 23 | // 가장 거리가 먼 책까지의 거리 24 | int largest = max(max_value, -min_value); 25 | 26 | // 최대 힙(Max Heap)을 이용 27 | for (int i = 0; i < n; i++) { 28 | // 책의 위치가 양수인 경우 29 | if (data[i] > 0) positive.push(data[i]); 30 | else negative.push(-data[i]); 31 | } 32 | 33 | while (!positive.empty()) { 34 | // 한 번에 m개씩 옮길 수 있으므로 m개씩 빼내기 35 | result += positive.top(); 36 | positive.pop(); 37 | for (int i = 0; i < m - 1; i++) { 38 | if (!positive.empty()) positive.pop(); 39 | } 40 | } 41 | 42 | while (!negative.empty()) { 43 | // 한 번에 m개씩 옮길 수 있으므로 m개씩 빼내기 44 | result += negative.top(); 45 | negative.pop(); 46 | for (int i = 0; i < m - 1; i++) { 47 | if (!negative.empty()) negative.pop(); 48 | } 49 | } 50 | 51 | // 일반적으로 왕복 거리를 계산하지만, 가장 먼 곳은 편도 거리 계산 52 | cout << result * 2 - largest; 53 | } 54 | -------------------------------------------------------------------------------- /Solutions/[18]_2.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.PriorityQueue; 3 | import java.util.Scanner; 4 | 5 | public class Main { 6 | 7 | public static void main(String[] args) { 8 | Scanner sc = new Scanner(System.in); 9 | 10 | int n = sc.nextInt(); 11 | int m = sc.nextInt(); 12 | int max_value = -10001; 13 | int min_value = 10001; 14 | 15 | ArrayList data = new ArrayList(); 16 | // 자바(Java)에서는 기본적으로 우선순위 큐가 최소 힙(Min Heap) 17 | PriorityQueue positive = new PriorityQueue(); 18 | PriorityQueue negative = new PriorityQueue(); 19 | 20 | // 모든 센서의 위치를 입력 받아 오름차순 정렬 21 | for (int i = 0; i < n; i++) { 22 | int x = sc.nextInt(); 23 | data.add(x); 24 | max_value = Math.max(max_value, x); 25 | min_value = Math.min(min_value, x); 26 | } 27 | 28 | // 가장 거리가 먼 책까지의 거리 29 | int largest = Math.max(max_value, -min_value); 30 | 31 | // 최대 힙(Max Heap)을 이용 32 | for (int i = 0; i < n; i++) { 33 | // 책의 위치가 양수인 경우 34 | if (data.get(i) > 0) positive.offer(-data.get(i)); 35 | // 책의 위치가 음수인 경우 36 | else negative.offer(data.get(i)); 37 | } 38 | 39 | int result = 0; 40 | while (!positive.isEmpty()) { 41 | // 한 번에 m개씩 옮길 수 있으므로 m개씩 빼내기 42 | result += positive.poll(); 43 | for (int i = 0; i < m - 1; i++) { 44 | if (!positive.isEmpty()) positive.poll(); 45 | } 46 | } 47 | while (!negative.isEmpty()) { 48 | // 한 번에 m개씩 옮길 수 있으므로 m개씩 빼내기 49 | result += negative.poll(); 50 | for (int i = 0; i < m - 1; i++) { 51 | if (!negative.isEmpty()) negative.poll(); 52 | } 53 | } 54 | 55 | // 일반적으로 왕복 거리를 계산하지만, 가장 먼 곳은 편도 거리 계산 56 | System.out.println(-result * 2 - largest); 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /Solutions/[18]_2.py: -------------------------------------------------------------------------------- 1 | import heapq 2 | 3 | n, m = map(int, input().split(' ')) 4 | array = list(map(int, input().split(' '))) 5 | positive = [] 6 | negative = [] 7 | 8 | # 가장 거리가 먼 책까지의 거리 9 | largest = max(max(array), - min(array)) 10 | 11 | # 최대 힙(Max Heap)을 위해 원소를 음수로 구성 12 | for i in array: 13 | # 책의 위치가 양수인 경우 14 | if i > 0: 15 | heapq.heappush(positive, -i) 16 | # 책의 위치가 음수인 경우 17 | else: 18 | heapq.heappush(negative, i) 19 | 20 | result = 0 21 | 22 | while positive: 23 | # 한 번에 m개씩 옮길 수 있으므로 m개씩 빼내기 24 | result += heapq.heappop(positive) 25 | for _ in range(m - 1): 26 | if positive: 27 | heapq.heappop(positive) 28 | 29 | while negative: 30 | # 한 번에 m개씩 옮길 수 있으므로 m개씩 빼내기 31 | result += heapq.heappop(negative) 32 | for _ in range(m - 1): 33 | if negative: 34 | heapq.heappop(negative) 35 | 36 | # 일반적으로 왕복 거리를 계산하지만, 가장 먼 곳은 편도 거리 계산 37 | print(-result * 2 - largest) -------------------------------------------------------------------------------- /Solutions/[18]_3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int n, result; 6 | vector > data; 7 | priority_queue pq; 8 | 9 | int main(void) { 10 | cin >> n; 11 | 12 | // 문제 정보를 입력 받은 이후에, 데드라인을 기준으로 정렬 13 | for (int i = 0; i < n; i++) { 14 | int x, y; 15 | cin >> x >> y; 16 | data.push_back(make_pair(x, y)); 17 | } 18 | 19 | sort(data.begin(), data.end()); 20 | 21 | for (int i = 0; i < n; i++) { 22 | int a = data[i].first; 23 | int b = data[i].second; 24 | pq.push(-b); 25 | // 데드라인을 초과하는 경우에는 최소 원소를 제거 26 | if (a < pq.size()) { 27 | pq.pop(); 28 | } 29 | } 30 | 31 | while (!pq.empty()) { 32 | result += -pq.top(); 33 | pq.pop(); 34 | } 35 | 36 | cout << result; 37 | } 38 | -------------------------------------------------------------------------------- /Solutions/[18]_3.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Collections; 3 | import java.util.Comparator; 4 | import java.util.PriorityQueue; 5 | import java.util.Scanner; 6 | 7 | class Node { 8 | public int x; 9 | public int y; 10 | Node (int x, int y) { 11 | this.x = x; 12 | this.y = y; 13 | } 14 | } 15 | 16 | public class Main { 17 | 18 | public static void main(String[] args) { 19 | Scanner sc = new Scanner(System.in); 20 | 21 | int n = sc.nextInt(); 22 | int result = 0; 23 | ArrayList data = new ArrayList(); 24 | PriorityQueue pq = new PriorityQueue(); 25 | 26 | // 문제 정보를 입력 받은 이후에, 데드라인을 기준으로 정렬 27 | for (int i = 0; i < n; i++) { 28 | data.add(new Node(sc.nextInt(), sc.nextInt())); 29 | } 30 | 31 | Collections.sort(data, new Comparator() { 32 | @Override 33 | public int compare(Node a, Node b) { 34 | if (a.x == b.x) { 35 | return Integer.compare(a.y, b.y); 36 | } 37 | return Integer.compare(a.x, b.x); 38 | } 39 | }); 40 | 41 | for (int i = 0; i < n; i++) { 42 | int a = data.get(i).x; 43 | int b = data.get(i).y; 44 | pq.offer(b); 45 | // 데드라인을 초과하는 경우에는 최소 원소를 제거 46 | if (a < pq.size()) { 47 | pq.poll(); 48 | } 49 | } 50 | 51 | while (!pq.isEmpty()) { 52 | result += pq.poll(); 53 | } 54 | 55 | System.out.println(result); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /Solutions/[18]_3.py: -------------------------------------------------------------------------------- 1 | import heapq 2 | 3 | n = int(input()) 4 | array = [] 5 | q = [] 6 | 7 | # 문제 정보를 입력 받은 이후에, 데드라인을 기준으로 정렬 8 | for i in range(n): 9 | a, b = map(int, input().split(' ')) 10 | array.append((a, b)) 11 | array.sort() 12 | 13 | for i in array: 14 | a = i[0] 15 | heapq.heappush(q, i[1]) 16 | # 데드라인을 초과하는 경우에는 최소 원소를 제거 17 | if a < len(q): 18 | heapq.heappop(q) 19 | 20 | print(sum(q)) -------------------------------------------------------------------------------- /Solutions/[19]_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int n, result; 6 | int row[15]; 7 | 8 | // x번째 행에 놓은 Queen에 대하여 검증 9 | bool check(int x) { 10 | // 이전 행에서 놓았던 모든 Queen들을 확인 11 | for (int i = 0; i < x; i++) { 12 | // 위쪽 혹은 대각선을 확인 13 | if (row[x] == row[i]) return false; 14 | if (abs(row[x] - row[i]) == x - i) return false; 15 | } 16 | return true; 17 | } 18 | 19 | // x번째 행에 대하여 처리 20 | void dfs(int x) { 21 | if (x == n) result += 1; 22 | else { 23 | // x번째 행의 각 열에 Queen을 둔다고 가정 24 | for (int i = 0; i < n; i++) { 25 | row[x] = i; 26 | // 해당 위치에 Queen을 두어도 괜찮은 경우 27 | if (check(x)) { 28 | // 다음 행으로 넘어가기 29 | dfs(x + 1); 30 | } 31 | } 32 | } 33 | } 34 | 35 | int main() { 36 | cin >> n; 37 | dfs(0); 38 | cout << result; 39 | } 40 | -------------------------------------------------------------------------------- /Solutions/[19]_1.py: -------------------------------------------------------------------------------- 1 | # x번째 행에 놓은 Queen에 대해서 검증 2 | def check(x): 3 | # 이전 행에서 놓았던 모든 Queen들을 확인 4 | for i in range(x): 5 | # 위쪽 혹은 대각선을 확인 6 | if row[x] == row[i]: 7 | return False 8 | if abs(row[x] - row[i]) == x - i: 9 | return False 10 | return True 11 | 12 | # x번째 행에 대하여 처리 13 | def dfs(x): 14 | global result 15 | if x == n: 16 | result += 1 17 | else: 18 | # x번째 행의 각 열에 Queen을 둔다고 가정 19 | for i in range(n): 20 | row[x] = i 21 | # 해당 위치에 Queen을 두어도 괜찮은 경우 22 | if check(x): 23 | # 다음 행으로 넘어가기 24 | dfs(x + 1) 25 | 26 | n = int(input()) 27 | row = [0] * n 28 | result = 0 29 | dfs(0) 30 | print(result) -------------------------------------------------------------------------------- /Solutions/[19]_2.py: -------------------------------------------------------------------------------- 1 | # 이동 좌표 (상, 하, 좌, 우) 2 | dx = [-1, 1, 0, 0] 3 | dy = [0, 0, -1, 1] 4 | 5 | def bfs(x, y): 6 | global result 7 | # 동일한 경우는 한 번만 계산하기 위하여 집합(Set) 자료형 사용 8 | q = set() 9 | q.add((x, y, array[x][y])) 10 | 11 | while q: 12 | x, y, step = q.pop() 13 | # 가장 긴 이동 거리를 저장 14 | result = max(result, len(step)) 15 | 16 | # 네 방향 (상, 하, 좌, 우)으로 이동하는 경우를 각각 확인 17 | for i in range(4): 18 | nx = x + dx[i] 19 | ny = y + dy[i] 20 | 21 | # 이동할 수 있는 위치이면서, 새로운 알파벳인 경우 22 | if (0 <= nx and nx < r and 0 <= ny and ny < c and 23 | array[nx][ny] not in step): 24 | q.add((nx, ny, step + array[nx][ny])) 25 | 26 | # 전체 보드 데이터를 입력 받습니다. 27 | r, c = map(int, input().split()) 28 | array = [] 29 | for _ in range(r): 30 | array.append(input()) 31 | 32 | # 백트래킹 수행 결과를 출력합니다. 33 | result = 0 34 | bfs(0, 0) 35 | print(result) -------------------------------------------------------------------------------- /Solutions/[19]_3.py: -------------------------------------------------------------------------------- 1 | import copy 2 | 3 | result = [] 4 | string = [] 5 | visited = [] 6 | 7 | # 조합(Combination) 함수 구현 8 | def combination(array, length, index): 9 | # 길이가 length인 모든 조합 찾기 10 | if len(string) == length: 11 | result.append(copy.deepcopy(string)) 12 | return 13 | # 각 원소를 한 번씩만 뽑도록 구성 14 | for i in range(index, len(array)): 15 | if i in visited: 16 | continue 17 | string.append(array[i]) 18 | visited.append(i) 19 | combination(array, length, i + 1) 20 | string.pop() 21 | visited.pop() 22 | 23 | vowels = ('a', 'e', 'i', 'o', 'u') 24 | l, c = map(int, input().split(' ')) 25 | 26 | # 가능한 암호를 사전식으로 출력해야 하므로 정렬 수행 27 | array = input().split(' ') 28 | array.sort() 29 | 30 | combination(array, l, 0) 31 | 32 | # 길이가 l인 모든 암호 조합을 확인 33 | for password in result: 34 | # 모음의 개수를 세기 35 | count = 0 36 | for i in password: 37 | if i in vowels: 38 | count += 1 39 | 40 | # 최소 한 개의 모음과 최소 두 개의 자음이 있는 경우 출력 41 | if count >= 1 and count <= l - 2: 42 | print(''.join(password)) --------------------------------------------------------------------------------