Generating Sets (Codeforces 722D)
Tags: Greedy, trees

You are given a set Y of n distinct positive integers y1, y2, ..., yn.Set X of n distinct positive integers x1, x2, ..., xn is said to generate set Y if one can transform X to Y by applying some number of the following two operation to integers in X:  Take any integer xi and multiply it by two, i.e. replace xi with 2·xi.  Take any integer xi, multiply it by two and add one, i.e. replace xi with 2·xi + 1. Note that integers in X are not required to be distinct after each operation.Two sets of distinct integers X and Y are equal if they are equal as sets. In other words, if we write elements of the sets in the array in the increasing order, these arrays would be equal.Note, that any set of integers (or its permutation) generates itself.You are given a set Y and have to find a set X that generates Y and the maximum element of X is mininum possible.

Input: The first line of the input contains a single integer n (1 ≤ n ≤ 50 000) — the number of elements in Y.The second line contains n integers y1, ..., yn (1 ≤ yi ≤ 109), that are guaranteed to be distinct.

Output: Print n integers — set of distinct integers that generate Y and the maximum element of which is minimum possible. If there are several such sets, print any of them.


Sample Inputs:
5
1 2 3 4 5

6
15 14 3 13 1 12

6
9 7 13 17 5 11

Sample Outputs:
4 5 2 3 1

12 13 14 7 3 1

4 5 2 6 3 1

Solution (Python):
def main():
    from heapq import heapify, heapreplace
    input()
    s = set(map(int, input().split()))
    xx = [-x for x in s]
    heapify(xx)
    while True:
        x = -xx[0]
        while x != 1:
            x //= 2
            if x not in s:
                s.add(x)
                heapreplace(xx, -x)
                break
        else:
            break
    print(' '.join((str(-x) for x in xx)))
if __name__ == '__main__':
    main()

Solution (Python):
from heapq import heappop, heapify, heappush

def negative(num):
    return -num

def generator(nums):
    keeper = set(nums)
    nums = list(map(negative, nums))
    heapify(nums)
    answer = []
    while nums:
        top = -1 * heappop(nums)
        temp = top
        while top in keeper:
            top //= 2
        if top not in keeper and top != 0:
            heappush(nums, -1 * top)
            keeper -= {temp}
            keeper.add(top)
        else:
            answer.append(str(temp))
    return answer
length = int(input())
nums = list(map(int, input().split()))
print(' '.join(generator(nums)))

Solution (Python):
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = 'x' in file.mode or 'r' not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b'\n') + (not b)
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            (self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode('ascii'))
        self.read = lambda : self.buffer.read().decode('ascii')
        self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import Counter, defaultdict
import math

def matching(s):
    le = len(s)
    pi = [0] * le
    for i in range(1, le):
        j = pi[i - 1]
        while j > 0 and s[i] != s[j]:
            j = pi[j - 1]
        if s[i] == s[j]:
            j += 1
        pi[i] = j
    w = set()
    i = le - 1
    while pi[i] != 0:
        w.add(le - pi[i])
        i = pi[i] - 1
    return w
from heapq import heapify, heapreplace, heappop, heappush
input()
s = set(map(int, input().split()))
xx = [-x for x in s]
heapify(xx)
while True:
    x = -xx[0]
    while x != 1:
        x //= 2
        if x not in s:
            s.add(x)
            heappop(xx)
            heappush(xx, -x)
            break
    else:
        break
print(' '.join((str(-x) for x in xx)))