문제) 백준 - 동적 계획법 - 양팔저울
https://www.acmicpc.net/problem/2629
2629번: 양팔저울
첫째 줄에는 추의 개수가 자연수로 주어진다. 추의 개수는 30 이하이다. 둘째 줄에는 추의 무게들이 자연수로 가벼운 것부터 차례로 주어진다. 같은 무게의 추가 여러 개 있을 수도 있다. 추의 무
www.acmicpc.net
추의 인덱스와 한쪽 저울에 올린 무게를 memoization을 적용하여 동적 계획법을 진행합니다. 추를 올릴 경우, 추를 올리지 않을 경우, 반대쪽에 올릴 경우를 생각하여 해결합니다.
C++ 소스코드)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void solve(int idx, int total) { | |
if (idx > N) return; | |
int& ret = cache[idx][total]; | |
if (ret != -1) return; | |
cache[idx][total] = 1; | |
solve(idx + 1, total + chu[idx]); | |
solve(idx + 1, total); | |
solve(idx + 1, abs(total - chu[idx])); | |
return; | |
} |
Full Code)
GitHub - Chocochip101/BOJ_Solution: BOJ Solutions
BOJ Solutions. Contribute to Chocochip101/BOJ_Solution development by creating an account on GitHub.
github.com
반응형
'PS(Problem Solving) > 백준_BOJ' 카테고리의 다른 글
[백준] 10653번 - 마라톤 2 (C++) 문제 및 풀이 (0) | 2022.01.17 |
---|---|
[백준] 19942번 - 다이어트 (C++) 문제 및 풀이 (0) | 2022.01.16 |
[백준] 20365번 - 블로그2 (C++) 문제 및 풀이 (0) | 2022.01.14 |
[백준] 16432번 - 떡장수와 호랑이 (C++) 문제 및 풀이 (0) | 2022.01.13 |
[백준] 15658번 - 연산자 끼워넣기 (2) (C++) 문제 및 풀이 (0) | 2022.01.13 |
댓글