You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求解Codeforces 281D:数组子序列前两大元素最大异或值

Hey there! Let's clear up the confusion around this problem and break down exactly why the optimal solution works—since I know you've been tinkering with sorting and stacks without hitting the right approach, and the official solution code feels opaque.

First, let's make sure we're on the same page about the problem's key detail, because this changes everything:

  • If the problem asks for any non-contiguous subsequence, then it simplifies to finding the maximum XOR of any pair of elements in the array. Why? For any two elements x and y, you can just take the subsequence containing only x and y—the larger one is the first maximum, the smaller is the second, so their XOR is a valid candidate.
  • If it asks for contiguous subarrays, then stacks are exactly the right tool, which is likely what the official solution uses.

Let's break down both scenarios, starting with the stack-based subarray case since that's what you experimented with.

Stack-Based Solution for Subarrays

The core idea here is that in any contiguous subarray, the maximum and second maximum elements are tied to each other via "greater neighbors"—elements that bound the range where one is the maximum. A monotonic decreasing stack helps us efficiently track these pairs as we iterate.

Here's a step-by-step breakdown of the official approach:

  1. Maintain a monotonic decreasing stack: Every element in the stack is larger than the elements that come after it (from bottom to top). This lets us quickly access the largest elements seen so far.
  2. For each element x in the array:
    • While the stack isn't empty, compute x ^ stack.top() and update your maximum XOR value. This is valid because the stack's top is the current largest element, so in some subarray, these two would be the top two maximums.
    • If the stack's top element is smaller than x, pop it. Why? Because x is larger, so any future subarray including the popped element would have x as a better maximum candidate, making the popped element irrelevant for future max-second max pairs.
    • Stop popping once the stack's top is larger than x (to keep the stack monotonic), then push x onto the stack.

Example Walkthrough

Take the array [3,1,4] (the first sample input):

  • Start with empty stack. Push 3. Max XOR is 0.
  • Next element 1:
    • Compute 1^3=2 → max becomes 2.
    • 3 > 1, so stop popping. Push 1. Stack is [3,1].
  • Next element 4:
    • Compute 4^1=5 → max becomes 5.
    • 1 <4, pop it. Now stack has [3].
    • Compute 4^3=7 → max becomes 7.
    • 3 <4, pop it. Stack is empty. Push 4.
  • Final max is 7, which matches the sample answer.

Trie Solution for Subsequences

If the problem allows non-contiguous subsequences, the optimal approach uses a binary trie to find the maximum XOR pair in O(n log M) time (where M is the maximum element value):

  1. Build a trie: Insert each element's binary representation into the trie, starting from the highest bit (e.g., 30th bit for 32-bit integers).
  2. Find maximum XOR for each element: For each element, traverse the trie to find the bit pattern that flips as many bits of the current element as possible (since XOR is maximized when bits are different).
  3. Track the maximum XOR value found across all elements.

Why Your Initial Approaches Didn't Work

  • Sorting: Sorting doesn't help because the maximum XOR doesn't come from elements close in value. For example, 5^2=7 is larger than 5^4=1, even though 4 is closer to 5 in sorted order.
  • Regular stacks: A standard stack doesn't maintain the monotonic order needed to efficiently track valid max-second max pairs. Only a monotonic stack can keep elements in a sequence that lets you quickly access relevant pairs.

内容的提问来源于stack exchange,提问作者Aarish Ramesh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:48:05