-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
2-kokeunho #5
2-kokeunho #5
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
κ³ μνμ ¨μ΅λλ€~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
λμ κ³νλ²μ λ°°μ°κΈ° μ’μ μμ κ°μ΅λλ€. μμ μκ³ λ¦¬μ¦ κ°μ λ bruteforceλ‘λ νμ΄λ΄€κ³ dpλ‘λ νμ΄λ΄€λ κΈ°μ΅μ΄ λλ€μ.
μμ μ λ°±μ€μμλ κ°μ λ¬Έμ λ₯Ό νμ΄λ΄€μμ΅λλ€. μ½λ μΌλΆλ₯Ό 보λ c++λ‘ νμ΄ν μ μ½λλ κ±°μ μ μ¬νλ€μ.
int solution(int N, int K, vector<int> W, vector<int> V) {
vector<vector<int> > opt(N + 1, vector<int>(K + 1, 0));
for (int i = 1; i <= N; i++) {
for (int w = 0; w <= K; w++) {
if (w < W[i - 1])
opt[i][w] = opt[i - 1][w];
else
opt[i][w] = max(opt[i - 1][w], V[i - 1] + opt[i - 1][w - W[i - 1]]);
}
}
return opt[N][K];
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
κ³ μνμ ¨μ΅λλ€. λ무 λ§μ΄ κΉλ¨Ήμλ knapsackμ λλΆμ λ€μ 곡λΆνκ² λμμ΅λλ€ :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ λ λ‘μ§μ λΉμ·νκ² κ΅¬ννμ΅λλ€. cppλ‘ νλ 0-based λ°°μ΄μ΄λ 1-basedλ°°μ΄ λλ¬Έμ μ‘°κΈ ν·κ°λ¦¬λ€μ.
μ 체 μ½λ
#include <iostream>
#include <vector>
using namespace std;
int n, k;
vector<int> weight;
vector<int> value;
void solution(vector<vector<int>>& dp) {
for (int i = 0; i <= k; i++) dp[0][i] = 0;
for (int i = 1; i <= n; i++) {
for (int w = 1; w <= k; w++) {
if (weight[i-1] > w)
dp[i][w] = dp[i - 1][w];
else
dp[i][w] = max(dp[i - 1][w],
value[i-1] + dp[i - 1][w - weight[i-1]]);
}
}
}
int main() {
cin >> n >> k;
int w, v;
weight.reserve(n);
value.reserve(n);
for (int i = 0; i < n; i++) {
cin >> w >> v;
weight.push_back(w);
value.push_back(v);
}
vector<vector<int>> dp(n+1, vector<int>(k+1));
solution(dp);
cout << dp[n][k];
return 0;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<details>
<summary>μ¬κΈ°κ° μ λͺ© </summary>
```
CODE!
```
</details>
μμ κ°μ΄ μμ±νμλ©΄ μ 체 μ½λλ₯Ό ν κΈλ‘ λ£μ μ μμ΅λλ€!! μ°Έκ³ νμΈμ©
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ€ λ§ν¬μ κΏν κ°μ¬ν©λλ€
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
λ°°λλ¬Έμ μ€λλ§μ 보λκΉ λ§μ΄ νκ°λ Έμ΅λλ€.
νμκ° μ λ κ±Έλ¦°κ²κ°μμ
νμ΄μ¬ code
N, K = map(int, input().split())
items = [tuple(map(int, input().split())) for _ in range(N)]
def baenang(K, items):
dp = [0] * (K + 1)
for w, v in items:
for k in range(K, w-1, -1):
dp[k] = max(dp[k], dp[k-w] + v)
return dp[K]
ans = baenang(K, items)
print(ans)
2μ€ forλ¬Έμ λλλ°, 첫λ²μ§Έ forλ¬Έμ λλλ itemsμμ 물건νλλΉ νλ² λλκ²μ΄κ³
λ΄λΆ forλ¬Έμ ν λ¬Όκ±΄λΉ λ°°λμ 무κ²κ° KλΆν° 0κΉμ§ μ€μ΄λ€λ λ§μ½ μλ‘μ΄ λ¬Όκ±΄μ λ£κ³ κ·Έ valueμ dp[λ¨μ무κ²]λ₯Ό λν κ°μ΄ λ ν¬λ€λ©΄ μλ μ‘°ν©μμ μ μ‘°ν©μΌλ‘ λ°κΏμ£Όλ κ²μ
λλ€.
μ΄λ κ² λͺ¨λ 물건μ λ°λ³΅νλ©΄ κ°λ°©μ μ΅λλ¬΄κ² dp[7]μλ μ΅μ μ μ‘°ν©λ§μ΄ λ¨κ²λ©λλ€.
π λ¬Έμ λ§ν¬
[BOJ νλ²ν λ°°λ] https://www.acmicpc.net/problem/12865
βοΈ μμλ μκ°
1h30m
β¨ μλ μ½λ
2μ°¨μ λ°°μ΄ dp ν μ΄λΈ dp[N+1][K+1]μ λ§λλλ€.
(N+1, K+1λ‘ ν, μ΄μ λ§λλ μ΄μ λ κ°λ°©μ μ무κ²λ λ€μ§ μμμ λλ₯Ό νμνκΈ° μν¨μ λλ€. )
iλ²μ§Έ μμ΄ν μ 무κ²(w)κ° νμ¬ κ°λ°©μ νμ© λ¬΄κ²(k)λ³΄λ€ μκ±°λ κ°λ€λ©΄ ν΄λΉ μμ΄ν μ λ£μμ§ λ§μ§ νλ¨ν©λλ€.
μ΄λ κΈ°μ‘΄μ κ°λ°©μ λ€μ΄μλ μμ΄ν μ κ°μΉ(v)μ λΉκ΅νμ¬ κ°μΉκ° λμ μͺ½μ μ νν©λλ€.
λ°±μ€ μ μΆλ ₯ μμ μμ λ§λ€μ΄μ§λ dpν μ΄λΈ κ²°κ³Όμ λλ€(μμ΄ν¨λκ° μμ΄μ γ γ ..)
μ΅μ’ μ μΌλ‘ dp[N][K]μλ μ΅λ κ°μΉκ°μ΄ μ μ₯λ©λλ€.
π μλ‘κ² μκ²λ λ΄μ©
λ€μ΄λλ―Ή νλ‘κ·Έλλ°(dp)μ λν΄ μκ² λμμ΅λλ€.
1μ°¨μ κ· νΈμ BFS λ¬Έμ λλΆμ μ΄ λ¬Έμ λ₯Ό μ²μ λ΄€μ λ BFSλ₯Ό λ μ¬λ Έμ΅λλ€. ν΄λΉ λ¬Έμ λ₯Ό BFS, DFSλ‘ ν μλ μλ€κ³ ν©λλ€.
νμ§λ§ μ€λ³΅κ³μ°μΌλ‘ κ³μ°λμ΄ λ무 λ§μμ΅λλ€. μ΄λ¬ν κ²½μ°μ dpλ₯Ό μ¬μ©νλ€κ³ νλλ°μ.
dpλ₯Ό μ¬μ©νμ¬ μμ μ¬μ§μμ λ³Ό μ μλ―μ΄ dp[i][j]μ κΈ°λκ°μ κ³μ°νκΈ° μν΄ dp[i-1][j]μ dp[i-1][j-weight] + valueλ§ λΉκ΅ν΄λ³΄λ©΄ λ©λλ€.
μκ³ λ¦¬μ¦ μ΄λ³΄λΌ dp μ€λͺ μ μ΄ν΄νλλ° μκ°μ λ§μ΄ λ λ Έμ΅λλ€...
μ¬μ€ μμ§ μ λλ‘ μ΄ν΄νλ€κ³ ν μ μμ κ² κ°μ΅λλ€. κ·Έλμ PRλ ν‘μ€μμ€μΈ κ² κ°λ€μ;
μ μ¬ λ¬Έμ λ₯Ό λ§μ΄ νμ΄λ³΄λλ‘ νκ² μ΅λλ€
dp μ°Έκ³ μμ: https://youtu.be/0bqfTzpWySY?si=AisTguK8A6F2xuOR