[20250508] BOJ / P1 / 고양이 소개팅 / 권혁준 #329
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/16858
🧭 풀이 시간
50분
👀 체감 난이도
✏️ 문제 설명
캣타워에 N개의 보금자리가 있고, 각 보금자리 i에는 C[i]마리의 암컷 or 수컷 고양이가 살고 있다. 보금자리들은 트리 구조를 이룬다.
수컷 고양이는 최대 높이 제한 V[i] 이내에 있는 암컷 고양이에게 도달하면 소개팅이 이루어진다.
성사될 수 있는 소개팅 횟수의 최댓값을 구해보자.
🔍 풀이 방법
[사용한 알고리즘]
수컷을 아래로 떨어뜨리는 대신, 암컷이 올라가는 형태로 생각해도 무방하다.
그리디 관찰
암컷 고양이를 올리면서 이동 거리를 구해놓고, 수컷 고양이 중 이 이동거리에 높이 제한이 가장 가까운 고양이와 소개팅을 하는 것이 항상 이득이다.
그 전에 미리 소개팅을 해버리면, 더 위에 있는 수컷 고양이와 배정될 기회를 잃어버리기 때문이다.
암컷 고양이 관리
리프 노드부터 간선을 타고 올라가며 암컷 고양이의 정보를 관리한다.
암컷 고양이가 올라온 거리 및 그 개체수를 저장해야 하므로, Map으로 관리한다. (key = 거리, value = 개체수)
두 Map을 합칠 때는 Smaller to Larger 테크닉으로 합친다. 시간복잡도 =$O(N \log^2{N})$
소개팅 성사시키기
수컷 고양이가 있는 정점에 도달했다면, Map에서 이분 탐색(lower bound, lower 등)으로 성사 가능한 암컷 고양이의 정보를 찾고, 정답을 갱신시킨다.
⏳ 회고
컨테이너의 iterator에 대한 깊은 이해가 필요했다.
진짜 어렵다