[20250211] BOJ / 플래3 / XOR 합 / 권혁준 #83
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/13504
🧭 풀이 시간
25분
👀 체감 난이도
✏️ 문제 설명
🔍 풀이 방법
문제를 조금 바꿔보자.
어떤 수열 B에서, 어떤 수$x$ 와 XOR했을 때 가장 큰 값을 구하려고 한다. (즉, $\max(B_i \oplus x)$ 를 구하려고 한다.)
이 문제는, 이진수 트라이를 이용하면 가장 큰 값을$O(\log{\max(B)})$ 의 시간에 구할 수 있다.
트라이의 최상위 노드(루트)를 깊이 31로 설정해놓고, 내려갈수록 깊이가 줄어든다고 하자.
각 노드는 왼쪽 자식 혹은 오른쪽 자식을 가질 수 있으며, 각각 비트 0 혹은 1을 의미한다.
수열의 원소를 트라이에 모두 삽입하고, 최댓값을 구할 때는 정수 x에 대해 트라이의 루트에서 출발하여, 최대한 다른 비트로 가도록 했을 때 나온 값이 답이 된다.
이 문제에서는, 연속 부분 수열의 최대 XOR을 찾아야 하고, XOR의 성질에 의해$(A_1 \oplus \cdots \oplus A_i) \oplus (A_1 \oplus \cdots \oplus A_k) = A_{k+1} \oplus \cdots \oplus A_i$ 가 된다. (단, $k < i$ )
수열$S$ 를 새로 만들어 $S_i = A_1 \oplus \cdots \oplus A_i$ 로 정의하면, 수열 $S$ 에서 위 문제를 푸는 것과 동일하게 변한다.
⏳ 회고
이진수 트라이를 오랜만에 구현해서 좀 복잡하게 짠 것 같다. 분명히 더 쉽게 짜는 법이 있을 것 같은데 나중에 비슷한 문제가 나오면 더 최적화를 해봐야겠다