Skip to content

Commit 4359f16

Browse files
committed
2 parents 63b362f + 419932a commit 4359f16

File tree

9 files changed

+269
-24
lines changed

9 files changed

+269
-24
lines changed

2주차 Knapsack/README.md

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -82,58 +82,59 @@ f(C) = \begin{cases}
8282
\end{cases}
8383
$$
8484

85-
* 한 번 투자했던 도시에 다시 투자하는 것이 가능하다.
86-
* 적어도 $C$명을 확보하는 것이므로, 그 이상의 숫자의 고객을 확보해도 된다.
85+
- 한 번 투자했던 도시에 다시 투자하는 것이 가능하다.
86+
- 적어도 $C$명을 확보하는 것이므로, 그 이상의 숫자의 고객을 확보해도 된다.
8787

8888
#### 시간 복잡도
8989

90-
* 메모이제이션 없이 수행한다면, 최악의 경우 : $O(N^C)$
91-
* 그럼 메모이제이션을 한다면 시간 복잡도는 어떻게 될까?
90+
- 메모이제이션 없이 수행한다면, 최악의 경우 : $O(N^C)$
91+
- 그럼 메모이제이션을 한다면 시간 복잡도는 어떻게 될까?
9292

9393
## 찾아온 문제들
9494

9595
#### 김동주
9696

9797
| 난이도 | 번호 | 제목 | 링크 | 비고 |
9898
| ------ | ---- | ----------- | --------------------- | ---- |
99-
| ![S1] | 1149 | RGB거리 | <https://boj.kr/1149> | DP |
100-
| ![G4] | 2133 | 타일 채우기 | <https://boj.kr/2133> | DP |
99+
| ![S1] | 1149 | RGB거리 | <https://boj.kr/1149> | #dp |
100+
| ![G4] | 2133 | 타일 채우기 | <https://boj.kr/2133> | #dp |
101101

102102
#### 정우현
103103

104-
| 난이도 | 번호 | 제목 | 링크 |
105-
| ------ | ---- | ---- | ------------------ |
106-
| ![??] | - | - | <https://boj.kr/-> |
104+
| 난이도 | 번호 | 제목 | 링크 | 비고 |
105+
| ------ | ----- | ------ | ---------------------- | ----------------------- |
106+
| ![S1] | 16113 | 시그널 | <https://boj.kr/16113> | #string #implementation |
107107

108108
#### 서동혁
109109

110-
| 난이도 | 번호 | 제목 | 링크 |
111-
| ------ | ---- | ---- | ------------------ |
112-
| ![??] | - | - | <https://boj.kr/-> |
110+
| 난이도 | 번호 | 제목 | 링크 | 비고 |
111+
| ------ | ---- | ------------- | --------------------- | ----------------------------- |
112+
| ![G4] | 1715 | 카드 정렬하기 | <https://boj.kr/1715> | #greedy #priority_queue(heap) |
113113

114114
#### 손영준
115115

116-
| 난이도 | 번호 | 제목 | 링크 |
117-
| ------ | ---- | ---- | ------------------ |
118-
| ![??] | - | - | <https://boj.kr/-> |
116+
| 난이도 | 번호 | 제목 | 링크 |비고 |
117+
| ------ | ---- | ---- | ------------------ | ----------------------------|
118+
| ![G4] | 10830 | 행렬 제곱 | <https://boj.kr/10830> | #Divide and Conquer |
119119

120120
#### 권동균
121121

122-
| 난이도 | 번호 | 제목 | 링크 |
123-
| ------ | ---- | ---- | ------------------ |
124-
| ![??] | - | - | <https://boj.kr/-> |
122+
| 난이도 | 번호 | 제목 | 링크 | 비고 |
123+
| ------ | ----- | --------- | ---------------------- | --------- |
124+
| ![S2] | 1260 | DFS와 BFS | <https://boj.kr/1260> | #bfs #dfs |
125+
| ![G5] | 10026 | 적록색약 | <https://boj.kr/10026> | #bfs |
125126

126127
#### 박준석
127128

128-
| 난이도 | 번호 | 제목 | 링크 |
129-
| ------ | ---- | ---- | ------------------ |
130-
| ![??] | - | - | <https://boj.kr/-> |
129+
| 난이도 | 번호 | 제목 | 링크 | 비고 |
130+
| ------ | ---- | ---- | --------------------- | -------------------- |
131+
| ![S1] | 2002 | 추월 | <https://boj.kr/2002> | #queue #hashing #set |
131132

132133
#### 양효인
133134

134-
| 난이도 | 번호 | 제목 | 링크 |
135-
| ------ | ---- | ---- | ------------------ |
136-
| ![??] | - | - | <https://boj.kr/-> |
135+
| 난이도 | 번호 | 제목 | 링크 | 비고 |
136+
| ------ | ----- | ---------- | ---------------------- | -------------------------------- |
137+
| ![S2] | 12867 | n차원 여행 | <https://boj.kr/12867> | #hashing #coordinate_compression |
137138

138139
<!-- solved.ac 문제 난이도 별 태그 이미지 -->
139140

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# N차원 여행
2+
3+
4+
MAX_M = 50
5+
6+
N = int(input())
7+
M = int(input())
8+
indicies = list(map(int, input().split()))
9+
steps = [(1 if c == '+' else -1) for c in input().strip()]
10+
11+
hash_index = {
12+
idx: i
13+
for i, idx in enumerate(set(indicies))
14+
}
15+
16+
try:
17+
vector_history = [[0] * M]
18+
for i in range(M):
19+
vector = vector_history[-1].copy()
20+
idx = hash_index[indicies[i]]
21+
vector[idx] = vector_history[-1][idx] + steps[i]
22+
# 해당 vector를 이미 방문한 적 있는지 검사
23+
for j in range(i):
24+
if vector_history[j] == vector:
25+
raise ValueError
26+
vector_history.append(vector)
27+
except ValueError:
28+
print(0)
29+
else:
30+
print(1)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# 카드 정렬하기
2+
3+
import sys
4+
from heapq import heappop, heappush, heapify
5+
6+
7+
N, *heap = map(int, sys.stdin.read().split())
8+
9+
heapify(heap)
10+
n_total_compares = 0
11+
12+
while len(heap) > 1:
13+
n_compares = heappop(heap) + heappop(heap)
14+
heappush(heap, n_compares)
15+
n_total_compares += n_compares
16+
17+
print(n_total_compares)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# 추월
2+
3+
from collections import deque
4+
import sys
5+
6+
7+
N = int(sys.stdin.readline())
8+
9+
# 터널은 사실상 FIFO 구조.
10+
tunnel = deque()
11+
overspeeded_cars = set()
12+
13+
# 터널에 들어가는 차량을 Queue에 넣음
14+
for i in range(N):
15+
car = sys.stdin.readline().strip()
16+
tunnel.append(car)
17+
18+
# 터널에서 나온 차량을 Queue에서 제거해가며 비교
19+
for i in range(N):
20+
# 터널에서 나온 차량
21+
car = sys.stdin.readline().strip()
22+
23+
# 이미 터널을 나왔던 (과속) 차량들은 Queue에서 제거
24+
while tunnel[0] in overspeeded_cars:
25+
tunnel.popleft()
26+
27+
if tunnel[0] == car:
28+
tunnel.popleft()
29+
else:
30+
# 나와야 할 순서대로 나온 것이 아닌 경우 = 추월한 경우
31+
overspeeded_cars.add(car)
32+
33+
print(len(overspeeded_cars))

2주차 Knapsack/박준석/1106.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <iostream>
2+
#include <algorithm>
3+
4+
using namespace std;
5+
6+
int main() {
7+
ios::sync_with_stdio(false);
8+
cin.tie(NULL);
9+
cout.tie(NULL);
10+
11+
int C, N;
12+
cin >> C >> N;
13+
14+
int dp[100001] = { 0, };
15+
16+
int cost[20] = { 0, };
17+
int cust[20] = { 0, };
18+
for (int i = 0; i < N; i++) {
19+
cin >> cost[i] >> cust[i];
20+
}
21+
//input
22+
23+
for (int i = 0; i < N; i++) {
24+
for (int j = 0; j < 100001; j++) {
25+
if (j - cost[i] >= 0) //i번째 값을 빼도 0보다 같거나 크다면
26+
dp[j] = max(dp[j], dp[j - cost[i]] + cust[i]); // 0/1 중 큰것을 선택
27+
}
28+
}
29+
30+
for (int j = 0; j < 100001; j++) {
31+
if (dp[j] >= C) {
32+
cout << j;
33+
break;
34+
}
35+
}
36+
37+
}

2주차 Knapsack/박준석/1535.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <iostream>
2+
#include <algorithm>
3+
4+
using namespace std;
5+
6+
int L[20] = { 0, };
7+
int J[20] = { 0, };
8+
int N;
9+
int mem[20][101] = {0,};
10+
11+
int knap(int i, int hp) {
12+
if (hp <= 0) //죽으면 -100
13+
return -100;
14+
if (i == N) //다 돌면 끝
15+
return 0;
16+
if (mem[i][hp] != -1) // 이미 계산
17+
return mem[i][hp];
18+
mem[i][hp] = max((knap(i + 1, hp - L[i]) + J[i]), knap(i + 1, hp));
19+
return mem[i][hp];
20+
}
21+
22+
int main() {
23+
ios::sync_with_stdio(false);
24+
cin.tie(NULL);
25+
cout.tie(NULL);
26+
27+
cin >> N;
28+
29+
for (int i = 0; i < N; i++) {
30+
cin >> L[i];
31+
}
32+
for (int i = 0; i < N; i++) {
33+
cin >> J[i];
34+
}
35+
for (int i = 0; i < 20; i++) {
36+
for (int j = 0; j < 101; j++) {
37+
mem[i][j] = -1;
38+
}
39+
}
40+
41+
cout << knap(0, 100);
42+
43+
44+
}

2주차 Knapsack/손영준/1535.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
n = int(input()) #사람의 수
2+
H = [0] + list(map(int, input().split())) #health
3+
J = [0] + list(map(int, input().split())) #joyful
4+
# list 1부터 시작.
5+
6+
#dp 테이블
7+
dp = [[0]* 101 for _ in range(n+1)]
8+
9+
for x in range(1,n+1):
10+
h = H[x]
11+
j = J[x]
12+
for y in range(1,101):
13+
if y < h: #현재 사람 x
14+
dp[x][y] = dp[x-1][y]
15+
else: #현재 사람 o
16+
dp[x][y] = max(dp[x-1][y], dp[x-1][y-h]+ j)
17+
18+
print(dp[n][99])

2주차 Knapsack/양효인/1106.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int c, n;
5+
int input[20][2];
6+
int dp[100001];
7+
8+
int countDP(int customer, int cityNum) {
9+
int min = 100 * 1000;
10+
int cost;
11+
if (customer <= 0) return 0;
12+
else if (dp[customer] > 0) return dp[customer];
13+
else {
14+
for (int i = 0; i < cityNum; i++) {
15+
cost = countDP(customer - input[i][1], cityNum) + input[i][0];
16+
min = cost < min ? cost : min;
17+
}
18+
dp[customer] = min;
19+
return min;
20+
}
21+
}
22+
23+
int main() {
24+
ios::sync_with_stdio(false);
25+
cin.tie(0); cout.tie(0);
26+
27+
cin >> c >> n;
28+
for (int i = 0; i < n; i++) {
29+
int a, b;
30+
cin >> a >> b;
31+
input[i][0] = a;
32+
input[i][1] = b;
33+
}
34+
35+
cout << countDP(c, n);
36+
37+
return 0;
38+
39+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
C,N = map(int,input().split())
2+
arr_mony = []
3+
arr_piple =[]
4+
5+
for i in range(N):
6+
a,b = map(int,input().split())
7+
arr_mony.append(a)
8+
arr_piple.append(b)
9+
10+
result = []
11+
12+
def minmony(i,mony):
13+
if i >= C:
14+
result.append(mony)
15+
if i == N:
16+
i = 0
17+
return max(arr_piple[i] + minmony(i+1, mony + arr_mony[i]), minmony(i+1,mony), arr_mony[i] + minmony(i, mony+arr_mony[i]))
18+
19+
20+
21+
minmony(0,0)
22+
result.sort()
23+
print(result[0])
24+
25+
#넣는다. 안넣는다. 현재를 넣는다.
26+
#그리고 비용과 값을 같이 넣어서 비용이 가장 작은 것을 넣는다.

0 commit comments

Comments
 (0)