Блог пользователя In_GENEraL

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

D. New Year's Problem

This can be solved easily with binary search on the min-max value.

Binary Search Function

While solving this in the contest, I wasn't able to come up with this approach. Rather I tried solving the same binary search function "possible" with a different approach and this lead me to a different version of the problem.

Task

Given an array of n positive integers $$$a_{1}$$$,$$$a_{2}$$$,...$$$a_{n}$$$ and an integer $$$m$$$ where $$$m$$$ is a power of 2. Determine the minimum number of numbers to be selected from the array so that their binary $$$OR$$$ will be $$$m-1$$$. If there doesn't exist a solution, output -1.

Input Format

$$$n$$$ $$$m$$$

$$$a_{1}$$$ $$$a_{2}$$$ ... $$$a_{n}$$$

Constraints

  • $$$1$$$ < $$$n$$$ < $$$10^{5}$$$

  • $$$1$$$ < $$$m$$$ < $$$2^{10}$$$

  • $$$0$$$ <= $$$a_{i}$$$ <= $$$m$$$

Sample Input 1

3 32
10 21 31

Sample Output 1

1

Explanation

10 - 0  1  0  1  0
21 - 1  0  1  0  1
31 - 1  1  1  1  1
Selecting 31 will be enough.

Sample Input 2

5 32
9 25 27 10 21

Sample Output 2

2

Explanation

10 - 0  1  0  0  1
21 - 1  1  0  0  1
31 - 1  1  0  1  1
10 - 0  1  0  1  0
21 - 1  0  1  0  1

10 | 21 = 31. So answer will be 2.

I was not able to solve this and also tried searching for the same type of problem on the internet but failed to find one. If anyone has solved this before or has any thoughts of any complexity of the solution, please let me know.

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

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

[DELETED as it was wrong solution]

  • »
    »
    3 года назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится
    Sample Input 3. 
    3 256
    171 240 15
    
    Sample Output 3.
    2
    
    Explanation
    171 - 1 0 1 0 1 0 1 1
    240 - 1 1 1 1 0 0 0 0
    15  - 0 0 0 0 1 1 1 1
    
    
    It is enough to pick 240 and 15 in order to get 255 i.e. 240 | 15.
    
    

    Edit 1: Your output is 3.

    Edit 2: Regarding the similarity of the problem, if we know what is the minimum number of numbers we need to get in order to find $$$m-1$$$ i.e. all 1's, then we can use this in the binary search function to calculate the (true, false) for a particular min-max value.

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

      [AGAIN WRONG SOLUTION, maybe I am dumb or problem is way too hard ]

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

        No. I don't think the greedy approach would solve the problem.

        Sample Input 4. 
        3 64
        41 36 27
        
        Sample Output 4.
        2
        
        Explanation
        41 - 1 0 1 0 0 1
        36 - 1 0 0 1 0 0
        27 - 0 1 1 0 1 1
        
        
        It is enough to pick 36 and 27 in order to get 63 i.e. 36 | 27.
        
        

        Your output is 3.

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

          Then there is a DP solution with O(m) space but TC will be O(n*m) which is not feasible :(
          Edit : It may be feasible with $$$ m <= \exp(2,11) $$$ but any further increase in power will not work

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

            Like considering all the subsets but optimizing it with dp?

            • »
              »
              »
              »
              »
              »
              »
              3 года назад, # ^ |
              Rev. 4   Проголосовать: нравится 0 Проголосовать: не нравится
              DP Solution