[20250720] BOJ / G4 / 다이어트 / 이강현 #507
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🧷 문제 링크
https://www.acmicpc.net/problem/19942
🧭 풀이 시간
40분
👀 체감 난이도
✏️ 문제 설명
재료 개수가 여러 개 있고 영양성분들과 가격이 나타나 있음.
근데 최소로 섭취해야하는 영양성분들이 주어짐.
이를 만족하면서 최소 비용으로 재료를 조합할때, 그 비용과 그 조합을 출력.
🔍 풀이 방법
비트마스킹
N이 15이하라서 비트마스킹으로 풀었다.
⏳ 회고
기본적인 비트마스킹 문제, 근데 한 가지 함정이 있었다.
비트마스킹 탐색을 적은 비트부터해서 비용이 작은 경우만 업데이트해서 자연스럽게 재료 사전 순이 맞춰질 줄 알았다.
하지만 예를 들어 3번째 재료가 필요하고 1번째, 2번째 재료는 모두 영양성분이 0일때, 원래 코드로는 3만 나왔는데 재료의 개수는 전혀 중요하지 않고 비용이 중요하기 때문에 영양성분이 모두 0인 1번째 재료와 2번째 재료를 포함해서 1 2 3으로 출력하는게 사전순 앞서기때문에 이러한 문자열 비교 로직을 넣어줘야 했다.
사전 순 이야기가 나오는데 숫자 비교가 이루어질 때, 문자열 비교를 생각해보자.