Color Palette
Time limit3sMemory limit1024 MB
Maintain a set of K-bit colors under insertions and, for each query color, return the stored color with the maximum number of matching bit positions, breaking ties by smallest value.
- Level
Medium7 of 10
- Topics
- Trie, Bit manipulation, Greedy, Brute force
- Solved
- No attempts yet
Problem
A company that makes graphics software is developing a new graphics program. One of its modules manages a color palette. When the program starts, the palette is empty. The user can add new colors to the palette, or ask which of the palette's colors is most similar to a given color.
Colors are represented as -bit integers (values from to ), and the similarity of two colors is the number of bit positions where their binary representations agree. For example, when , the similarity of colors 00110 and 10101 is , because only the second and third bits from the left have the same value.
The program behaves like a palette-management module built from the following operations, and it prints a log in the order the operations are called.
Input
The first line of the input contains the number of bits () used to represent colors and the number of operations (). Each of the following lines contains two integers and (). means "add color to the palette", and means "find the best match for color in the palette".
Output
Print the log of the operations the module processed. First print init(K, N). Then process each operation in the given order and print the following.
- For an operation that adds color , print
add(C). - For an operation that looks up color , print
find(C) = R, where is the palette color whose similarity to (the number of matching bits) is largest. If several colors are equally similar, is the smallest such color value.
Finally print done(). A find operation is given only when the palette already contains at least one color.