Skip to content
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

Merged
merged 2 commits into from
Oct 17, 2024
Merged

2-kokeunho #5

merged 2 commits into from
Oct 17, 2024

Conversation

kokeunho
Copy link
Collaborator

@kokeunho kokeunho commented Oct 1, 2024

πŸ”— 문제 링크

[BOJ ν‰λ²”ν•œ λ°°λ‚­] https://www.acmicpc.net/problem/12865

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

1h30m

✨ μˆ˜λ„ μ½”λ“œ

2차원 λ°°μ—΄ dp ν…Œμ΄λΈ” dp[N+1][K+1]을 λ§Œλ“­λ‹ˆλ‹€.
(N+1, K+1둜 ν–‰, 열을 λ§Œλ“œλŠ” μ΄μœ λŠ” 가방에 아무것도 듀지 μ•Šμ•˜μ„ λ•Œλ₯Ό ν‘œμ‹œν•˜κΈ° μœ„ν•¨μž…λ‹ˆλ‹€. )
i번째 μ•„μ΄ν…œμ˜ 무게(w)κ°€ ν˜„μž¬ κ°€λ°©μ˜ ν—ˆμš© 무게(k)보닀 μž‘κ±°λ‚˜ κ°™λ‹€λ©΄ ν•΄λ‹Ή μ•„μ΄ν…œμ„ 넣을지 말지 νŒλ‹¨ν•©λ‹ˆλ‹€.
μ΄λ•Œ 기쑴에 가방에 λ“€μ–΄μžˆλ˜ μ•„μ΄ν…œμ˜ κ°€μΉ˜(v)와 λΉ„κ΅ν•˜μ—¬ κ°€μΉ˜κ°€ 높은 μͺ½μ„ μ„ νƒν•©λ‹ˆλ‹€.
image
λ°±μ€€ μž…μΆœλ ₯ μ˜ˆμ œμ—μ„œ λ§Œλ“€μ–΄μ§€λŠ” 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

Copy link
Collaborator

@kangrae-jo kangrae-jo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

κ³ μƒν•˜μ…¨μŠ΅λ‹ˆλ‹€~

Copy link
Collaborator

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];
}

Copy link
Collaborator

@g0rnn g0rnn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

κ³ μƒν•˜μ…¨μŠ΅λ‹ˆλ‹€. λ„ˆλ¬΄ 많이 κΉŒλ¨Ήμ—ˆλ˜ knapsack을 덕뢄에 λ‹€μ‹œ κ³΅λΆ€ν•˜κ²Œ λ˜μ—ˆμŠ΅λ‹ˆλ‹€ :)

Copy link
Collaborator

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;
}

Copy link
Collaborator

@g0rnn g0rnn Oct 5, 2024

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>

μœ„μ™€ 같이 μž‘μ„±ν•˜μ‹œλ©΄ 전체 μ½”λ“œλ₯Ό ν† κΈ€λ‘œ 넣을 수 μžˆμŠ΅λ‹ˆλ‹€!! μ°Έκ³ ν•˜μ„Έμš©

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

였 λ§ˆν¬μ—… κΏ€νŒ κ°μ‚¬ν•©λ‹ˆλ‹€

wnsmir

This comment was marked as resolved.

Copy link
Collaborator

@wnsmir wnsmir left a 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)
μ €μ˜ μ½”λ“œμž…λ‹ˆλ‹€. 쌍으둜 정해진 값이라 뢈러였기 μ‰½κ²Œ νŠœν”Œμ„ μ‚¬μš©ν•΄ λ°›μ•„μ£Όμ—ˆκ³  동적배열을 μ‚¬μš©ν• κ²ƒμ΄κΈ° 떄문에 λ¬΄κ²ŒλŠ” 0λΆ€ν„° 7κΉŒμ§€ 2μ°¨μ›λ°°μ—΄λ‘œ μž‘μ•„μ€λ‹ˆλ‹€.(λ°°μ—΄μ˜ μΈλ±μŠ€κ°€ 무게, λ°°μ—΄μ•ˆμ˜ 값이 value)

2쀑 for문을 λ„λŠ”λ°, 첫번째 for문을 λŒλ•ŒλŠ” itemsμ—μ„œ λ¬Όκ±΄ν•˜λ‚˜λ‹Ή ν•œλ²ˆ λ„λŠ”κ²ƒμ΄κ³ 
λ‚΄λΆ€ for문은 ν•œ 물건당 λ°°λ‚­μ˜ λ¬΄κ²Œκ°€ KλΆ€ν„° 0κΉŒμ§€ μ€„μ–΄λ“€λ•Œ λ§Œμ•½ μƒˆλ‘œμš΄ 물건을 λ„£κ³  κ·Έ value와 dp[λ‚¨μ€λ¬΄κ²Œ]λ₯Ό λ”ν•œ 값이 더 크닀면 μ›λž˜ μ‘°ν•©μ—μ„œ μƒˆ μ‘°ν•©μœΌλ‘œ λ°”κΏ”μ£ΌλŠ” κ²ƒμž…λ‹ˆλ‹€.

μ΄λ ‡κ²Œ λͺ¨λ“  물건을 λ°˜λ³΅ν•˜λ©΄ κ°€λ°©μ˜ μ΅œλŒ€λ¬΄κ²Œ dp[7]μ—λŠ” 졜적의 μ‘°ν•©λ§Œμ΄ λ‚¨κ²Œλ©λ‹ˆλ‹€.

@kokeunho kokeunho merged commit 6870c3d into main Oct 17, 2024
@g0rnn g0rnn deleted the 2-kokeunho branch October 30, 2024 10:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants