[20250914] BOJ / P5 / 서커스 나이트 / 권혁준 #890
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/26077
🧭 풀이 시간
20분
👀 체감 난이도
✏️ 문제 설명
돌고래 N마리가 있고, 각 돌고래는 ID를 가진다.
돌고래끼리는 아래 조건에 따라 의사소통을 할 수 있다.
처음 메시지를 전달받는 돌고래가 i일 때, 메시지를 전달받을 수 있는 돌고래 수의 최댓값을 k[i]라고 하자.
max(k[1], ..., k[N])을 구해보자.
🔍 풀이 방법
주파수로 가능한 정수는 소수만 고려해도 충분하다.
각 ID를 소인수분해해서 나오는 정수들과 분리 집합으로 이어주면, 같은 컴포넌트 끼리는 메시지를 주고받을 수 있게 된다.
초기 ID의 개수만 기록해두면, 컴포넌트 크기를 구하는 것도 가능하고, union이 모두 끝난 뒤에 반복문 한 번 돌면서 max를 구해줬다.
처음엔 그냥$O(\sqrt{N})$ 으로 소인수분해 했더니 시간 초과가 났다.$O(N\sqrt{N})$ 이 되니까 당연한 결과인 것 같다.
지금 생각해보면 전체 시간복잡도가
에라토스테네스의 체를 응용해서 e[n]에는 n을 나눌 수 있는 최소 소인수를 저장해서 소인수분해하는 방식으로 해결했다. 이렇게 하면 소인수분해에 걸리는 시간이$O(\log{N})$ 이 됨.
⏳ 회고
eezz