-
Notifications
You must be signed in to change notification settings - Fork 1
10-csecaujunseo #38
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
10-csecaujunseo #38
Conversation
dohyeondol1
left a comment
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.
1λ‘ λ§λ€κΈ°...
μ²μμ 그리λλ‘ μ κ·Όνλ€κ° λν¨λ₯Ό λ³΄κ³ μ²μ DPλ₯Ό λ°°μ°κ² νλ λ¬Έμ μΈκ² κΈ°μ΅λλ€μ..
μ λ κ°μ λ°©μμΌλ‘ ν΄κ²°νμ΅λλ€.
κΈ°λ³Έμ μΌλ‘ dp[i] = dp[i-1] + 1λ‘ μ€μ νκ³ ,
3 λλ 2λ‘ λλμ΄μ§λ€λ©΄ dp[i/3]+1κ³Ό κΈ°μ‘΄μ dp[i]λ₯Ό λΉκ΅(2λ κ°μ λ°©μ)νμ¬ μ΅μκ°μ μ μ₯νλ νμ΄μ
λλ€.
C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> dp(N+1);
dp[1] = 0;
for (int i = 2; 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);
}
cout << dp[N] << '\n';
return 0;
}μΆκ°λ‘, μ€μλμ μ½λλ₯Ό μ κ° μ‘°κΈ λ κ°λ¨νκ² μμ ν΄λ³΄μμ΅λλ€.
C99
#include <stdio.h>
int main(){
int dp[1000001], n;
scanf("%d", &n);
dp[1] = 0;
for(int i = 2; i <= n; i++){
dp[i] = dp[i-1] + 1;
if(i%2 == 0 && dp[i] > dp[i/2]+1) dp[i] = dp[i/2]+1;
if(i%3 == 0 && dp[i] > dp[i/3]+1) dp[i] = dp[i/3]+1;
}
printf("%d", dp[n]);
}μ€νμΌ μ°¨μ΄μΈ λΆλΆλ μμ§λ§,
λΆνμν λ©λͺ¨λ¦¬ ν λΉ μ²΄ν¬ μ½λλ λ°°μ΄ λ©λͺ¨λ¦¬ λμ ν λΉ μ½λλ₯Ό μ§μλ΄€μ΄μ.
μ무λλ λ¬Έμ λ₯Ό ν λ, μ§§μ μ½λ μμ±μ μκ°μ λ¨μΆμμΌμ€μ μ΄λμ λ μ΄μ μ΄ μλ κ² κ°μ΅λλ€.
λ¬Όλ‘ μκΈ°κ° μ μΌ μ μ°λ λ°©λ²μ΄ μ₯λ‘μ
λλ€ γ
γ
μ΄ λ¬Έμ λ λΉμ·ν 1, 2, 3 λνκΈ° λΌλ λ¬Έμ λ νλ² νμ΄λ³΄μλ κ±Έ μΆμ²λ립λλ€!
DP λ¬Έμ λ μ νμλ§ μΈμΈ μ μμΌλ©΄ μ½κ² ν리λλ° κ·Έ μ νμμ΄... νμ λ¬Έμ μΈ κ² κ°μμ γ
γ
;;
caucsejunseo/README.md
Outdated
| | 8μ°¨μ | 2025.04.11 | μ°κ²°λ¦¬μ€νΈ| [ν€λ‘κ±°](https://www.acmicpc.net/problem/5397)|https://github.com/AlgoLeadMe/AlgoLeadMe-13/pull/30| | ||
| | 8μ°¨μ | 2025.04.11 | μ°κ²°λ¦¬μ€νΈ| [ν€λ‘κ±°](https://www.acmicpc.net/problem/5397)|https://github.com/AlgoLeadMe/AlgoLeadMe-13/pull/30| | ||
| | 9μ°¨μ | 2025.04.30 | λ¬Έμμ΄ | [λ£λ³΄μ‘](https://www.acmicpc.net/problem/1764)|https://github.com/AlgoLeadMe/AlgoLeadMe-13/pull/35| | ||
| |10μ°¨μ| 2025.05.05 | λ€μ΄λλ―Ή | [1λ‘λ§λ€κΈ°](https://www.acmicpc.net/problem/1463)|| |
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.
| |10μ°¨μ| 2025.05.05 | λ€μ΄λλ―Ή | [1λ‘λ§λ€κΈ°](https://www.acmicpc.net/problem/1463)|| | |
| |10μ°¨μ| 2025.05.05 | DP | [1λ‘λ§λ€κΈ°](https://www.acmicpc.net/problem/1463)|https://github.com/AlgoLeadMe/AlgoLeadMe-13/pull/38| |
PR λ§ν¬κ° λΉ μ Έμλ€μ! λν μ νμ 'λ€μ΄λλ―Ή' 보λ€λ DP, λ€μ΄λλ―Ή νλ‘κ·Έλλ°, λμ κ³νλ² μ λκ° λ μ μ ν κ² κ°μ΅λλ€.
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.
mallocμ λμ λ©λͺ¨λ¦¬ ν λΉμ μ¬μ©νλ ν¨μμΈλ°, MAX κ°μ μμλ‘ μ ν΄λκ³ μ΄λ₯Ό κΈ°λ°μΌλ‘ ν λΉνλ κ²μ μ¬μ€μ μ μ λ©λͺ¨λ¦¬ ν λΉκ³Ό λ€λ₯Ό λ° μμ΄ λ³΄μ΄λ€μ..! μ μ λ©λͺ¨λ¦¬ ν λΉμΌλ‘λ μΆ©λΆν ν΄κ²°ν μ μλ λ¬Έμ λΌμ λ€μκ³Ό κ°μ΄ μμ νλ©΄ ν¨μ¬ κ°λ¨ν μ½λκ° λ κ² κ°μ΅λλ€.
| int* dp = (int*)malloc(sizeof(int) * MAX); | |
| int dp[1000001]; |
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.
첨μΈνμλ©΄, ν¨μ λ΄μμ ν° μ¬μ΄μ¦μ λ°°μ΄μ μ μΈνκ² λ κ²½μ° Stack λ©λͺ¨λ¦¬ μ΄κ³Όκ° λ°μν μ μμ΅λλ€. κ³ μ λ ν° ν¬κΈ°μ λ°°μ΄μ μ μΈνκ³ μΆμΌλ©΄ μ μ λ³μλ‘ λλ κ² μ’μ΅λλ€ :)
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.
μ΄ μ½λλ λμ λ©λͺ¨λ¦¬ ν λΉ μ€ν¨μ λλΉνκΈ° μν΄μ μμ±νμ κ² κ°μλ°, μ΄ κ²½μ°μλ ν λΉ ν¬κΈ°κ° κ³ μ λ μμ(MAX)μ΄κ³ ν¬κΈ°λ λΉκ΅μ μμμ μΌλ°μ μΈ νκ²½μμλ ν λΉ μ€ν¨κ° λ°μν κ°λ₯μ±μ΄ κ±°μ μλ€κ³ λ΄λ λ©λλ€. λ°±μ€μμλ μ₯κ°ν΄μ λ©λλ©. μ§μλ 무방ν κ² κ°λ€μ.
9kyo-hwang
left a comment
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.
μ΄ λ¬Έμ λ DPλ‘ ν μ μμ§λ§, κ·Έλν λ¬Έμ λ‘ ν΄μν΄μλ ν μ μμ΅λλ€.
X λ
Έλμμ μΆλ°ν΄ 1 λ
Έλμ λλ¬νλ "μ΅λ¨ κ²½λ‘" μ°ΎκΈ° λ¬Έμ λ‘ ν΄μνλ©΄ λ± κ·Έλν λ¬Έμ κ°μ£ ?
#include <iostream>
#include <queue>
#include <unordered_map>
using namespace std;
int main()
{
cin.tie(nullptr)->sync_with_stdio(false);
int X; cin >> X;
queue<int> Q; Q.emplace(X);
unordered_map<int, int> Map; Map[X] = 0; // Visited λ°°μ΄μ΄λΌ μκ°νλ©΄ λ©λλ€.
while(!Q.empty())
{
int Current = Q.front(); Q.pop();
if(Current == 1)
{
break;
}
else if(Current < 1)
{
continue;
}
if(Current % 3 == 0 && Map.count(Current / 3) == 0) // μΈμ λ
Έλλ‘ κ° μ μλλ
{
Map[Current / 3] = Map[Current] + 1;
Q.emplace(Current / 3);
}
if(Current % 2 == 0 && Map.count(Current / 2) == 0) // λ°©λ¬Έν μ μ΄ μλ μ§ ν¨κ» 체ν¬
{
Map[Current / 2] = Map[Current] + 1;
Q.emplace(Current / 2);
}
if(Map.count(Current - 1) == 0)
{
Map[Current - 1] = Map[Current] + 1;
Q.emplace(Current - 1);
}
}
cout << Map[1];
return 0;
}
μ΄λ° μκ°μ λͺ»νλ€μ! μμ§ λ€μν λ¬Έμ λ₯Ό λͺ»νμ΄ λ΄μ μκ°μ΄ κ³ μ λλ κ±° κ°μ΅λλ€... λ€μμ κ·Έλν λ¬Έμ λ νμ΄λ³΄κ² μ΅λλ€! |
|
μμ dpμ κ·Έλνλ₯Ό μ΄μ©ν λ°©λ²μ μ μλ €μ£Όμ
μ μ μλ‘ μͺ½μΌλ‘λ μκ°ν΄λ΄€λλ°, import sys
def solve():
N = int(sys.stdin.readline())
if N == 1:
print(0)
return
dp = [0] * (N + 1)
for i in range(2, N + 1):
dp[i] = dp[i-1] + 1
if i % 2 == 0:
dp[i] = min(dp[i], dp[i//2] + 1)
if i % 3 == 0:
dp[i] = min(dp[i], dp[i//3] + 1)
print(dp[N])
if __name__ == '__main__':
solve() |
hadongun
left a comment
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.
μ΄ λ¬Έμ .. μ λ²μ νμλμ΄ μ¬λ°λ λ¬Έμ λΌλ©° λμ Έμ£Όμ
¨λ€κ° μ€ν¨ν λ¬Έμ λ€μ©
κ·Έλ₯ 그리λ μκ³ λ¦¬μ¦μΌλ‘ νλ €κ³ νλ κ² κ°μλ°, dpλ‘ νΈλ κ² μ μμ΄μκ΅°μ..!
κ³΅λΆ νκ³ μ½λ μ§λ΄€μ΅λλ΅
Details
n = int(input())
d = [0] * (n + 1)
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])
π λ¬Έμ λ§ν¬
1λ‘ λ§λ€κΈ°
βοΈ μμλ μκ°
1h
β¨ μλ μ½λ
μ΄μ μ νμλμ΄ νμ ¨λ dpλ¬Έμ λ₯Ό λ€μ νμ΄λ³΄μμ΅λλ€. #22
κ·Έλλ³΄λ€ ν¨μ¬ κ°λ¨ν λ¬Έμ μ λλ€.
μ΄ μΈκ°μ§ μ°μ°λ§ μ¬μ©νμ¬ κ°μ₯ μ μ μ°μ°μ μ¬μ©νλ λ°©λ²μ μ°Ύλ λ¬Έμ μ λλ€.
μ²μμ λ¨μν κ·Έλ₯ 3μΌλ‘ λ§μ΄ λλλ©΄ λλκ±° μλκ° μκ°νμλλ° κ²½μ°μ μκ° ν¨μ¬ λ§λλΌκ΅¬μ.
κ·Έλ¦¬κ³ κ°μ₯ ν° μλΆν° μλλ‘ λ΄λ €κ°λ©΄μ λ¬Έμ λ₯Ό ν΄κ²°νλ €κ³ νλ€λ³΄λ μ΄κ²λ κ²½μ°μ μκ° ν¨μ¬ λ§μμ μ€ν¨νμμ΅λλ€...
κ·Έλμ dp λ°©μμ μ¬μ©ν΄μ νκ² λμμ΅λλ€.

μ¬κΈ°μ dp[i]λ iλΌλ μ«μλ₯Ό 1λ‘ λ§λ€κΈ° μν μ΅μνμ μ°μ° νμλ₯Ό λ§ν©λλ€. dp[10]μ΄λ©΄ 3μΈκ±°μ£ .
dpλ μ΄μ μ 곡λΆνλ―μ΄ μ΄μ μ μ°μ°μ κΈ°μ΅νλ©° νΈλ λ°©μμ λλ€.
μ μ΄λ―Έμ§μ²λΌ μμ dp[1]λΆν° dp[9]κΉμ§λ ν΄κ²°λμλ€κ³ μκ°νκ³ κ·Έ μμ κ°λ€μ κΈ°μ΅νκ³ νΈλ λ°©μμ λλ€.
κ·Έλμ dp[10]μ ꡬν΄μΌ νλ€κ³ κ°μ νλ€λ©΄ dp[9]μ κ°μ κΈ°μ΅νκ³ μκ³ μμΌλ―λ‘ dp[10]μ dp[9]μμ μ°μ° νμμΈ +1μ ν dp[10]=dp[9]+1μ΄λΌλ μμ΄ μ±λ¦½ν μ μμ΅λλ€. λ λ€λ₯Έ μμ dp[10]=dp[5]+1μ΄ λ μ μμ΅λλ€. μ¦ 5λ₯Ό 1λ‘ λ§λλ μ°μ° νμμμ +1μ ν΄μ£Όλ©΄ λ©λλ€. μλνλ©΄ 10μ 2λ‘ λλ μ μκ³ κ·Έλ¬λ©΄ κ°μ΄ 5κ° λκΈ° λλ¬Έ!
μ΄κ±Έ μ νμμΌλ‘ μμ±νλ©΄
μ΄λ κ² λνλΌ μ μμ΅λλ€. κ·Έλ¬λκΉ μ²μλΆν° 10μμ 3λ λλ λ³΄κ³ 1λ λΉΌλ³΄κ³ νλκ² μλλΌ dp[1]λΆν° μ°¨λ‘λ‘ νμ΄λκ°λ λ°©μμ λλΉ
μλ μ½λ
'''μ λ ₯: μ μ N
λ°°μ΄ dp[1..N]μ μμ±
dp[1] = 0 // 1μ μ°μ° μμ΄ 1μ΄λ―λ‘ μ΄κΈ°κ°
for i = 2 to N:
μΆλ ₯: dp[N]
'''
π μλ‘κ² μκ²λ λ΄μ©
dpλ°©μμ λ€μ νλ² κ³΅λΆν΄λ³΄κ² λμμ΅λλ€!!