[20250507] BOJ / P1 / 닌자배치 / 권혁준 #327
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/4002
🧭 풀이 시간
30분
👀 체감 난이도
✏️ 문제 설명
N명의 닌자들은 마스터 닌자를 제외하고, 보스를 한 명씩 모신다. (트리 구조로 주어짐)
각 닌자들은 자신이 받는 월급이 정해져있고, 정수로 표현되는 리더십 레벨을 가진다.
닌자들 중 한 명을 매니저로 정해서, 매니저를 조상으로 갖는 모든 닌자들을 각각 자유롭게 고용할 수 있다.
단, 고용한 모든 닌자들에게 월급을 주어야 하며, 총 예산 M을 초과하면 안 된다.
만족도를 (고용한 닌자 수) * (매니저의 리더십 레벨)로 정의할 때, 만족도의 최댓값을 구해보자.
🔍 풀이 방법
[사용한 알고리즘]
월급이 더 적은 닌자부터 고용하는 것은 자명하다.
리프 노드에 있는 닌자부터 올라가며 각 닌자가 매니저일 때 고용한 닌자들을 우선순위 큐에 담아준다.
간선을 타고 올라갈 때마다, 두 우선순위 큐를 합칠 때 size가 작은 걸 큰 쪽에 붙이고(Smaller to Larger), 총 예산을 초과한다면 월급이 큰 닌자부터 우선순위 큐에서 제거한다.
각 닌자가 매니저일 때의 고용할 수 있는 최대 닌자 수는 그 시점의 큐의 size이므로 만족도를 계산할 수 있게 된다.
⏳ 회고
Java에서 참조형 변수에 대한 = 연산은 주소만 가져와서 시간복잡도가$O(1)$ 인 걸 알게 됐다.