Codeforces 722D. Generating Sets

D. Generating Sets
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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:

  1. Take any integer xi and multiply it by two, i.e. replace xi with xi.
  2. Take any integer xi, multiply it by two and add one, i.e. replace xi with 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.

Examples
input
5
1 2 3 4 5
output
4 5 2 3 1 
input
6
15 14 3 13 1 12
output
12 13 14 7 3 1 
input
6
9 7 13 17 5 11
output
4 5 2 6 3 1 

题目大意:给定一个集合Y,集合Y有一些1e9以内的正整数组成,求一个集合X,拥有和集合Y一多的元素,每一个在X集合中的元素可以*2或者*2+1,且进行任意次操作之后变为Y集合,求一种X集合使得X集合中的最大值最小


sol:显然贪心具有正确性,我们每次选取一个最大的数字,看它/2之后是否冲突,如果冲突就再/2,一直到不冲突,若一直冲突就结束。
用队来维护,复杂度O(n^(logn*logx,i))

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstdlib>
 5 #include<algorithm>
 6 #include<vector>
 7 #include<cmath>
 8 #include<ctime>
 9 #include<cstring>
10 #include<queue>
11 #include<set>
12 #define yyj(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout);
13 #define llg long long
14 #define maxn 200010
15 using namespace std;
16 llg i,j,k,n,m,x,a[maxn];
17 
18 struct node
19 {
20     long long val;
21     bool operator <(const node &rhs) const
22     {
23         return val < rhs.val;
24     }
25 };
26 
27 priority_queue<node> q;
28 set <node> s;
29 
30 int main()
31 {
32 //    yyj("d");
33     cin>>n;
34     node qq;
35     for (i=1;i<=n;i++)
36     {
37         scanf("%I64d",&x);
38         qq.val=x;
39         s.insert(qq);
40         q.push(qq);
41     }
42     node w;
43     while (1)
44     {
45         qq=q.top();
46         w.val=qq.val/2;
47         if (w.val==0) break;
48         while (s.find(w)!=s.end() && w.val/2!=0) w.val/=2;
49         while (s.find(w)==s.end())
50         {
51             s.erase(qq);
52             s.insert(w);
53             q.pop();
54             q.push(w);
55             qq=q.top(); w.val=qq.val/2;
56             if (w.val==0) break;
57             while (s.find(w)!=s.end() && w.val/2!=0) w.val/=2;
58         }
59         break;
60     }
61     while (!q.empty())
62     {
63         cout<<q.top().val<<" ";
64         q.pop();
65     }
66     return 0;
67 }
本文作者:xrdog 作者博客:http://www.cnblogs.com/Dragon-Light/ 转载请注明出处,侵权必究,保留最终解释权!
原文地址:https://www.cnblogs.com/Dragon-Light/p/5927519.html