[20250615] BOJ / G2 / 보석 도둑 / 이강현 #356
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/1202
🧭 풀이 시간
60분
👀 체감 난이도
✏️ 문제 설명
무게가 M이고 가격이 V인 보석을 훔칠건데 가방의 용량이 C임. 하나의 가방에는 하나의 보석만 들어갈 수 있음. 보석과 가방 목록이 주어질 때 훔칠 수 있는 보석 가격의 합의 최댓값을 구하기.
🔍 풀이 방법
그리디, 자료구조
처음에는 0-1 knapsack 문제인줄 알다가 가방의 수와 보석의 수가 300000인걸 보고 이거 그리디다라고 생각해서 둘다 정렬해놓고 그냥 넣었음.
근데 보석의 가치를 내림차순 정렬하는 건 맞는데 가방에 적절하게 넣는게 문제였음.
현재 보석이 가치는 크나 무게가 작은데 가장 큰 가방에 들어가면 손해니까
그래서 treeset을 써서 가방에서 보석 무게보다는 큰데 그중 가장 작은 것을 뽑고 넣었음.
근데 또 같은 크기의 가방인 경우는 treeset에서 중복이 안돼서 treemap으로 바꾸고 풀었음.
⏳ 회고
접근하는거랑 푸는 방법은 좋았는데 적절한 가방 선택하는거를 어캐할지 몰라서 method를 찾음. treeset은 ceiling, treemap은 ceilingKey라는 것이 있는데 이게 입력 인자보다 크거나 같지만 제일 작은거 하나 뽑아주는 거임. 시간 복잡도는 2logN