Блог пользователя Ibrahim-Elsayed

Автор Ibrahim-Elsayed, история, 3 года назад, По-английски

Given an array $$$A$$$ of $$$N$$$ non-negative integers, and two different integers $$$x$$$ and $$$y$$$, could you permute the array $$$A$$$ into a new array $$$B$$$ in such a way that after creating the $$$XOR$$$ prefix of the array $$$B$$$, both $$$x$$$ and $$$y$$$ would appear in it?

the $$$XOR$$$ of an empty prefix equals $$$0$$$.

Input: $$$n$$$, $$$x$$$ and $$$y$$$ ($$$2 \lt = n \lt = 30$$$), ($$$0 \lt = x,y \lt 2^{10}, x \ne y$$$)

then we are given an array $$$A$$$ with length $$$n$$$ $$$a_1, a_2, ..., a_n$$$ ($$$0 \lt = a_i \lt 2^{10}$$$)

test cases:

Input: $$$n=5$$$, $$$x=1$$$, $$$y=7$$$, $$$A = [1, 2, 3, 4, 5]$$$

Output: YES

Input: $$$n=3$$$, $$$x=8$$$, $$$y=9$$$, $$$A = [1, 2, 3]$$$

Output: NO

This was a problem in a national contest, we couldn't solve it. I would appreciate your help, thanks in advance.

  • Проголосовать: нравится
  • +28
  • Проголосовать: не нравится

»
3 года назад, скрыть # |
Rev. 3  
Проголосовать: нравится +17 Проголосовать: не нравится
Hint
Optimization

If you need further help to see the actual implementations of those ideas, mention it.

  • »
    »
    3 года назад, скрыть # ^ |
    Rev. 2  
    Проголосовать: нравится +5 Проголосовать: не нравится

    can you explain your solution more? the intended works in O(n * (max(Ai))^2). how to check for a pair in the first half if there is a matching that yields x and y?

    • »
      »
      »
      3 года назад, скрыть # ^ |
       
      Проголосовать: нравится +6 Проголосовать: не нравится

      Intended? So it seems either you were involved in the problem setting or contacted the problem setter. So ok give more details about the DP optimization. I am not so sure about it. Also, it wasn't my day so I just checked the post and helped with an idea I had upon reading the problem.

      The constraints allow complete searching with meet in middle(Divide A into A1, A2) (Safer option). I think u misunderstood the pairs part One from each half (A1, A2). Here is the code so you can better relate my words : code If you've a better idea on the optimization via DP, I'd like to hear 😅

»
3 года назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

We couldn't solve it too :( . But I guess we can use DP to solve it.

Prefix xor array will be like a1,a2,a2,...,A ,a6,a7, B ,a9,......a30

which mean that in the prefix xor array there should be prefix equals to A and B and XOR of elements between them equal to A^B.

We can divide the final prefix array into 3 segments

The first one contains elements their XOR value = A or B

The second one contains elements their XOR VALUE = A^B

The third contains useless elements (The elements we will skip in the dp function)

DP[31][2048][2048] This became a variation of subset xor problem. For each element, we can skip it or xor it with xorVal1 or xorVal2

if i==n+1 check if xorVal1 = A or B and xorVal2 = A^B

Please correct me if this is wrong.