1625D - Binary Spiders - CodeForces Solution


bitmasks data structures implementation math sortings trees *2300

Please click on ads to support us..

Python Code:

def main():

    n, k = readIntArr()
    a = readIntArr()

    if k == 0:
        print(n)
        assert n >= 2
        oneLineArrayPrint(list(range(1, n + 1)))
        return
    
    
    trie = [-1] * (300000 * 30 * 3 + 100)
    I = 3
    for idx, x in enumerate(a):
        u = 0
        for i in range(29, -1, -1):
            if (x & (1 << i)) > 0:
                if trie[u + 1] == -1:
                    trie[u + 1] = I
                    I += 3
                u = trie[u + 1]
            else:
                if trie[u] == -1:
                    trie[u] = I
                    I += 3
                u = trie[u]
                trie[u + 2] = idx  
    msb = -1
    for i in range(29, -1, -1):
        if (k & (1 << i)) > 0:
            msb = i
            break
    assert msb != -1

    ans = []
    st = [(0, 29)]
    while st:
        u, bit = st.pop()
        if bit > msb:
            if trie[u] != -1:
                st.append((trie[u], bit - 1))
            if trie[u + 1] != -1:
                st.append((trie[u + 1], bit - 1))
        else:
                        subtree_elements = []
            st2 = [(u, bit)]
            while st2:
                u2, bit2 = st2.pop()
                if bit2 == -1:
                    subtree_elements.append(trie[u2 + 2])
                else:
                    if trie[u2] != -1:
                        st2.append((trie[u2], bit2 - 1))
                    if trie[u2 + 1] != -1:
                        st2.append((trie[u2 + 1], bit2 - 1))

            best_xor = -1
            best_i1 = -1
            best_i2 = -1
            for i1 in subtree_elements:
                u2 = u
                for bit2 in range(bit, -1, -1):
                    if (a[i1] & (1 << bit2)) > 0:
                        if trie[u2] != -1:
                            u2 = trie[u2]
                        else:
                            u2 = trie[u2 + 1]
                    else:
                        if trie[u2 + 1] != -1:
                            u2 = trie[u2 + 1]
                        else:
                            u2 = trie[u2]
                i2 = trie[u2 + 2]
                xor = a[i1] ^ a[i2]
                if xor > best_xor:
                    best_xor = xor
                    best_i1 = i1
                    best_i2 = i2
                assert best_xor != -1
            if best_xor >= k:
                ans.append(best_i1)
                ans.append(best_i2)
            else:
                ans.append(best_i1)


    if len(ans) <= 1:
        print(-1)
    else:
        print(len(ans))
        for i in range(len(ans)):
            ans[i] += 1          oneLineArrayPrint(ans)

    return

import sys
input=sys.stdin.buffer.readline  
def oneLineArrayPrint(arr):
    print(' '.join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
    print('\n'.join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
    print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))
 
def readIntArr():
    return [int(x) for x in input().split()]
 
def makeArr(defaultValFactory,dimensionArr):     dv=defaultValFactory;da=dimensionArr
    if len(da)==1:return [dv() for _ in range(da[0])]
    else:return [makeArr(dv,da[1:]) for _ in range(da[0])]
 
def queryInteractive(a, b, c):
    print('? {} {} {}'.format(a, b, c))
    sys.stdout.flush()
    return int(input())
 
def answerInteractive(x1, x2):
    print('! {} {}'.format(x1, x2))
    sys.stdout.flush()
 
inf=float('inf')
 
from math import gcd,floor,ceil
import math
 
for _abc in range(1):
    main()


Comments

Submit
0 Comments
More Questions

905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
590. N-ary Tree Postorder Traversal
589. N-ary Tree Preorder Traversal
1299. Replace Elements with Greatest Element on Right Side
1768. Merge Strings Alternately
561. Array Partition I
1374. Generate a String With Characters That Have Odd Counts
1822. Sign of the Product of an Array
1464. Maximum Product of Two Elements in an Array
1323. Maximum 69 Number
832. Flipping an Image
1295. Find Numbers with Even Number of Digits
1704. Determine if String Halves Are Alike
1732. Find the Highest Altitude
709. To Lower Case
1688. Count of Matches in Tournament
1684. Count the Number of Consistent Strings
1588. Sum of All Odd Length Subarrays
1662. Check If Two String Arrays are Equivalent
1832. Check if the Sentence Is Pangram
1678. Goal Parser Interpretation
1389. Create Target Array in the Given Order
1313. Decompress Run-Length Encoded List
1281. Subtract the Product and Sum of Digits of an Integer
1342. Number of Steps to Reduce a Number to Zero
1528. Shuffle String