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

7-kangrae-jo #24

Merged
merged 2 commits into from
Nov 30, 2024
Merged

7-kangrae-jo #24

merged 2 commits into from
Nov 30, 2024

Conversation

kangrae-jo
Copy link
Collaborator

@kangrae-jo kangrae-jo commented Nov 12, 2024

πŸ”— 문제 링크

1둜 λ§Œλ“€κΈ°

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

27m

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

DP둜 λΆ„λ₯˜λœ 문제인 것을 μ•Œκ³  λ΄€μ§€λ§Œ,
κΆκΈˆν•΄μ„œ μ²˜μŒμ—λŠ” κ·Έλƒ₯ κ΅¬ν˜„μœΌλ‘œ ν’€μ–΄λ΄€μŠ΅λ‹ˆλ‹€.

if (N %= 3) N /= 3;
else if (N %= 2) N /= 2;
else N--;

μ΄λŸ°μ‹μœΌλ‘œμš”.

근데 λ¬Έμ œκ°€ μžˆλ‹€λŠ” 것을 μ•Œμ•˜μŠ΅λ‹ˆλ‹€.

3을 λ‚˜λˆ„κ±°λ‚˜ 2λ₯Ό λ‚˜λˆ„κ±°λ‚˜ 1을 λΉΌλŠ” 것에 μˆœμ„œκ°€ μƒκ²¨λ²„λ¦°λ‹€λŠ” μ μž…λ‹ˆλ‹€.
μœ„ μ½”λ“œλŒ€λ‘œλ©΄,
N = 10 μΌλ•Œ, 10 -> 5 -> 4 -> 2 -> 1 둜 5단계가 ν•„μš”ν•©λ‹ˆλ‹€.
근데 정닡은 , 10 -> 9 -> 3 -> 1 둜 4λ‹¨κ³„λ§Œ 있으면 되죠.

κ·Έλž˜μ„œ dp배열을 λ§Œλ“€μ–΄μ„œ if~else 문이 μ•„λ‹ˆλΌ,
if문으둜 λ§Œλ“€μ–΄ μ „λΆ€ κ²€μ‚¬ν•˜λ„λ‘ ν–ˆμŠ΅λ‹ˆλ‹€.
그리고 각 if λ¬Έμ—μ„œλŠ” min 값을 μ°Ύμ•„μ„œ dp배열에 μ €μž₯ν•˜λŠ” λ‘œμ§μ„ μΆ”κ°€ν–ˆμŠ΅λ‹ˆλ‹€.

DPλ¬Έμ œλŠ” 점화식이 ν•΅μ‹¬μ΄λ‹ˆ 점화식을 λ‚˜νƒ€λ‚΄κ³  pr λ§ˆμΉ˜κ² μŠ΅λ‹ˆλ‹€.

dp[i] = min(dp[i - 1] + 1, dp[i / 2] + 1, dp [i / 3] + 1)

(i/2, i/3에 λŒ€ν•΄μ„œλŠ” 각각 2와 3으둜 λ‚˜λˆ„μ–΄ λ–¨μ–΄μ Έμ•Ό 검사 κ°€λŠ₯)

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

κ·Έλƒ₯ μƒκ°λ‚˜λŠ” λŒ€λ‘œ λ¨Όμ € κ΅¬ν˜„μ„ ν•΄λ³΄λŠ” 것도 λ‚˜μ˜μ§€ μ•Šμ€ 방법인 것 κ°™μŠ΅λ‹ˆλ‹€.
처음 생각해낸 둜직의 λ¬Έμ œκ°€ 뭔지 λ³΄μ΄λ‹ˆκΉŒ,
μ •λ‹΅μœΌλ‘œ ν–₯ν•˜κΈ° μ‰¬μš΄ 것 κ°™κΈ° λ–„λ¬Έμž…λ‹ˆλ‹€.

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.

dpλŠ” 풀리면 정말 μž¬λ°ŒλŠ”κ±° κ°™λ„€μš” γ…‹γ…‹γ…‹
κ°•λž˜λ‹˜μ΄λž‘ λΉ„μŠ·ν•˜μ§€λ§Œ 쑰금 λ‹€λ₯Έ μ½”λ“œλ₯Ό κ°€μ Έμ™”μŠ΅λ‹ˆλ‹€. μ „λ°˜μ μΈ λ‘œμ§μ€ λΉ„μŠ·ν•˜λ‚˜ λ­”κ°€ κ΅¬ν˜„μ΄ naiveν•œκ±° κ°™λ„€μš” 😒

naive code
#include <iostream>
#include <algorithm>
#include <climits>
using namespace std;

int dp[1000001];

int divide3(int k) {
	if (k % 3 == 0) return k / 3;
	return 0;
}

int divide2(int k) {
	if (k % 2 == 0)return k / 2;
	return 0;
}

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

	int n;
	cin >> n;
	dp[0] = INT_MAX;
	dp[1] = 0;
	dp[2] = 1;
	dp[3] = 1;
	for (int i = 2; i <= n; i++) {
		dp[i] = min({ dp[i - 1], dp[divide2(i)], dp[divide3(i)] }) + 1;
	}
	cout << dp[n];
	return 0;
}


vector<int> dp(N + 1, 0);
dp[1] = 0;
dp[2] = 1;
Copy link
Collaborator

Choose a reason for hiding this comment

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

λ‹€μ‹œ μƒκ°ν•΄λ³΄λ‹ˆ ν•΄λ΄€μž dp[i-1]밖에 ν™•μΈμ•ˆν•˜λ‹ˆ μ΄ˆκΈ°ν™”λŠ” 2κΉŒμ§€λ©΄ μΆ©λΆ„ν•˜λ„€μš”

Comment on lines +15 to +19
for (int i = 3; i <= N; i++) {
dp[i] = dp[i-1] + 1;
if (i % 3 == 0) dp[i] = min(dp[i], dp[i / 3]+1);
if (i % 2 == 0) dp[i] = min(dp[i], dp[i / 2]+1);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

μ½”λ“œκ°€ 맀우 κ°„κ²°ν•˜λ„€μš”! μ–΄μ°¨ν”Ό κ²°κ΅­ dp[i]λ₯Ό setν•˜κ²Œ λ˜λ‹ˆ 미리 μ§€μ •ν•˜κ³  더 μž‘μ€ κ°’μœΌλ‘œ λΆ„λ₯˜ν•˜λ©΄ 이런 μ½”λ“œκ°€ λ‚˜μ˜¬ 수 μžˆλ„€μš”

@kokeunho
Copy link
Collaborator

DP λ¬Έμ œλŠ” 항상 λ™μž‘?이 μ‰½κ²Œ λ– μ˜€λ₯΄μ§€ μ•Šλ„€μš”..
점화식 보고 μ΄ν•΄ν•˜λŠ”λ° μͺΌκΈˆ κ±Έλ ΈμŠ΅λ‹ˆλ‹€ γ…Ž;
많이 ν’€μ–΄λ΄μ•Όκ² μŠ΅λ‹ˆλ‹€
이번 PR도 μˆ˜κ³ ν•˜μ…¨μŠ΅λ‹ˆλ‹€~

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.

DP문제λ₯Ό ν‘ΈλŠ” κ°€μž₯ 첫번째 λ‹¨κ³„λŠ” λ‹Ήμ—°ν•˜κ² μ§€λ§Œ 주어진 λ¬Έμ œκ°€ λ‹€μ΄λ‚˜λ―Ή ν”„λ‘œκ·Έλž˜λ° μœ ν˜•μž„μ„ νŒŒμ•…ν•˜λŠ”κ²ƒμ΄λΌ μƒκ°ν•©λ‹ˆλ‹€.

μ΄λ¬Έμ œλŠ” 각각의 연산방법이 주어지고, κ·Έ 방법듀을 μ΄μš©ν•΄ 1을 λ§Œλ“€μ–΄λ‚΄λŠ” λŒ€ν‘œμ μΈ DP의 μ˜ˆμ‹œ λ¬Έμ œμž…λ‹ˆλ‹€. DPκ³΅λΆ€ν• λ•Œ 제일처음 λ§Œλ‚¬λ˜ λ¬Έμ œμ˜€μ–΄μ„œ λΉ λ₯΄κ²Œ ν’€ 수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€.

+dpλ¬Έμ œλŠ” νƒ‘λ‹€μš΄(μž¬κ·€), λ°”ν…€μ—…(반볡) λ‘κ°€μ§€λ‘œ ν’€ 수 μžˆλŠ”λ° 반볡문으둜 ν‘ΈλŠ”κ²ƒμ΄ μ‹œκ°„λ³΅μž‘λ„λ©΄μ—μ„œλ‚˜ μžμ›ν• λ‹Ή λ©΄μ—μ„œ μ˜¬λ°”λ₯Έ ν’€μ΄λ²•μž…λ‹ˆλ‹€.

λ”°λΌμ„œ λ°”ν…€ μ—… λ°©μ‹μ˜ 반볡문 ν˜•νƒœλ‘œ 문제λ₯Ό ν’€μ–΄λ³΄μ•˜μŠ΅λ‹ˆλ‹€.

N = int(input())

d = [0]*10000001

for i in range(2, N+1):
    d[i] = d[i - 1] + 1

    if i%2 == 0:
        d[i] = min(d[i], d[i//2] + 1)
    
    if i%3 == 0:
        d[i] = min(d[i], d[i//3] + 1)

print(d[N])

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