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.
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.
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.
5 1 2 3 4 5
4 5 2 3 1
6 15 14 3 13 1 12
12 13 14 7 3 1
6 9 7 13 17 5 11
4 5 2 6 3 1
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 50050, M = 0, Z = 1e9 + 7, inf = 0x3f3f3f3f;
template <class T1, class T2>inline void gadd(T1 &a, T2 b) { a = (a + b) % Z; }
int n;
int a[N];
int b[N];
set<int>sot;
int main()
{
while (~scanf("%d", &n))
{
sot.clear();
for (int i = 1; i <= n; ++i)
{
int x; scanf("%d", &x);
sot.insert(x);
}
while (1)
{
int x = *--sot.end();
int y = x;
while (y)
{
y = y / 2;
if (!sot.count(y))break;
}
if (y == 0)break;
sot.erase(x);
sot.insert(y);
}
for (auto x : sot)printf("%d ", x);
puts("");
}
return 0;
}
/*
【题意】
给定n个不同y[]
让你输出一个x[],使得x[]两两不同,且x[]可以经过若干次*2或*2+1操作变为y[]
你构造的的x[]中,最大的x[i]要尽可能小
【类型】
贪心 暴力
【分析】
x变为y可能面临*2还是*2+1
但是y变为x只能/2
所以我们直接考虑把y变为x
显然只要把最大的y往小了缩就可以了。
一旦缩小到怎么缩小都会和其他数相同,说明有一个数字串已经从1 1x 1xx 1xxx 1xxxx 1xxxxx 都具备
也就是说,不光这个数无法缩小,这个数暂时不动,其他数缩小,也不会给他空出缩小的空间。
于是这个时候就可以停止操作,输出序列
【时间复杂度&&优化】
O(nlogn)
*/