Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
weeast1521 committed Jul 9, 2024
2 parents 63b362f + 419932a commit 4359f16
Show file tree
Hide file tree
Showing 9 changed files with 269 additions and 24 deletions.
49 changes: 25 additions & 24 deletions 2주차 Knapsack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,58 +82,59 @@ f(C) = \begin{cases}
\end{cases}
$$

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

#### 시간 복잡도

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

## 찾아온 문제들

#### 김동주

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

#### 정우현

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

#### 서동혁

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

#### 손영준

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

#### 권동균

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

#### 박준석

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

#### 양효인

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

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

Expand Down
30 changes: 30 additions & 0 deletions 2주차 Knapsack/김동주/extra/boj_12867.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# N차원 여행


MAX_M = 50

N = int(input())
M = int(input())
indicies = list(map(int, input().split()))
steps = [(1 if c == '+' else -1) for c in input().strip()]

hash_index = {
idx: i
for i, idx in enumerate(set(indicies))
}

try:
vector_history = [[0] * M]
for i in range(M):
vector = vector_history[-1].copy()
idx = hash_index[indicies[i]]
vector[idx] = vector_history[-1][idx] + steps[i]
# 해당 vector를 이미 방문한 적 있는지 검사
for j in range(i):
if vector_history[j] == vector:
raise ValueError
vector_history.append(vector)
except ValueError:
print(0)
else:
print(1)
17 changes: 17 additions & 0 deletions 2주차 Knapsack/김동주/extra/boj_1715.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# 카드 정렬하기

import sys
from heapq import heappop, heappush, heapify


N, *heap = map(int, sys.stdin.read().split())

heapify(heap)
n_total_compares = 0

while len(heap) > 1:
n_compares = heappop(heap) + heappop(heap)
heappush(heap, n_compares)
n_total_compares += n_compares

print(n_total_compares)
33 changes: 33 additions & 0 deletions 2주차 Knapsack/김동주/extra/boj_2002.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# 추월

from collections import deque
import sys


N = int(sys.stdin.readline())

# 터널은 사실상 FIFO 구조.
tunnel = deque()
overspeeded_cars = set()

# 터널에 들어가는 차량을 Queue에 넣음
for i in range(N):
car = sys.stdin.readline().strip()
tunnel.append(car)

# 터널에서 나온 차량을 Queue에서 제거해가며 비교
for i in range(N):
# 터널에서 나온 차량
car = sys.stdin.readline().strip()

# 이미 터널을 나왔던 (과속) 차량들은 Queue에서 제거
while tunnel[0] in overspeeded_cars:
tunnel.popleft()

if tunnel[0] == car:
tunnel.popleft()
else:
# 나와야 할 순서대로 나온 것이 아닌 경우 = 추월한 경우
overspeeded_cars.add(car)

print(len(overspeeded_cars))
37 changes: 37 additions & 0 deletions 2주차 Knapsack/박준석/1106.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <iostream>
#include <algorithm>

using namespace std;

int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);

int C, N;
cin >> C >> N;

int dp[100001] = { 0, };

int cost[20] = { 0, };
int cust[20] = { 0, };
for (int i = 0; i < N; i++) {
cin >> cost[i] >> cust[i];
}
//input

for (int i = 0; i < N; i++) {
for (int j = 0; j < 100001; j++) {
if (j - cost[i] >= 0) //i번째 값을 빼도 0보다 같거나 크다면
dp[j] = max(dp[j], dp[j - cost[i]] + cust[i]); // 0/1 중 큰것을 선택
}
}

for (int j = 0; j < 100001; j++) {
if (dp[j] >= C) {
cout << j;
break;
}
}

}
44 changes: 44 additions & 0 deletions 2주차 Knapsack/박준석/1535.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <iostream>
#include <algorithm>

using namespace std;

int L[20] = { 0, };
int J[20] = { 0, };
int N;
int mem[20][101] = {0,};

int knap(int i, int hp) {
if (hp <= 0) //죽으면 -100
return -100;
if (i == N) //다 돌면 끝
return 0;
if (mem[i][hp] != -1) // 이미 계산
return mem[i][hp];
mem[i][hp] = max((knap(i + 1, hp - L[i]) + J[i]), knap(i + 1, hp));
return mem[i][hp];
}

int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);

cin >> N;

for (int i = 0; i < N; i++) {
cin >> L[i];
}
for (int i = 0; i < N; i++) {
cin >> J[i];
}
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 101; j++) {
mem[i][j] = -1;
}
}

cout << knap(0, 100);


}
18 changes: 18 additions & 0 deletions 2주차 Knapsack/손영준/1535.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
n = int(input()) #사람의 수
H = [0] + list(map(int, input().split())) #health
J = [0] + list(map(int, input().split())) #joyful
# list 1부터 시작.

#dp 테이블
dp = [[0]* 101 for _ in range(n+1)]

for x in range(1,n+1):
h = H[x]
j = J[x]
for y in range(1,101):
if y < h: #현재 사람 x
dp[x][y] = dp[x-1][y]
else: #현재 사람 o
dp[x][y] = max(dp[x-1][y], dp[x-1][y-h]+ j)

print(dp[n][99])
39 changes: 39 additions & 0 deletions 2주차 Knapsack/양효인/1106.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <iostream>
using namespace std;

int c, n;
int input[20][2];
int dp[100001];

int countDP(int customer, int cityNum) {
int min = 100 * 1000;
int cost;
if (customer <= 0) return 0;
else if (dp[customer] > 0) return dp[customer];
else {
for (int i = 0; i < cityNum; i++) {
cost = countDP(customer - input[i][1], cityNum) + input[i][0];
min = cost < min ? cost : min;
}
dp[customer] = min;
return min;
}
}

int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);

cin >> c >> n;
for (int i = 0; i < n; i++) {
int a, b;
cin >> a >> b;
input[i][0] = a;
input[i][1] = b;
}

cout << countDP(c, n);

return 0;

}
26 changes: 26 additions & 0 deletions 2주차 Knapsack/정우현/1106_fail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
C,N = map(int,input().split())
arr_mony = []
arr_piple =[]

for i in range(N):
a,b = map(int,input().split())
arr_mony.append(a)
arr_piple.append(b)

result = []

def minmony(i,mony):
if i >= C:
result.append(mony)
if i == N:
i = 0
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]))



minmony(0,0)
result.sort()
print(result[0])

#넣는다. 안넣는다. 현재를 넣는다.
#그리고 비용과 값을 같이 넣어서 비용이 가장 작은 것을 넣는다.

0 comments on commit 4359f16

Please sign in to comment.