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 2·xi.
  2. 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.

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 

题意:如果你有一个原数列的话你可以对任何一个数字进行操作,令这个数字乘2或者乘2后在加1。现在给你一个目标数列,让你求一个原数列,这个原数列是所有可能的原数列当中最大的一个元素最小的那种。注意原数列和目标数列都必须满足set内元素的性质(即每个元素都不相同),但是在变化的过程中不需要满足这个条件。

思路:维护一个优先队列,把集合里的数都丢进去。最大的那个数除以二,查询是否与其他数重复。不重复,则此轮结束,继续下轮最大数除二做判断;重复,则继续除二,直到找到合适的或者一直找不到,后者结束,序列即为所求。这里有个要点,是最大数除二,能停则停,换个思路,一直除到底,会占据个位置,可能让比它更大的数没办法占位置,从而算法失败,测试例子最后一个即可以验证。

代码:

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
#include <set> #define LEN 50000 int n;
std::set<int> s; void input();
void work();
void output(); int main(){
input();
work();
output(); return ;
} void input(){
int tmp;
scanf("%d", &n);
for(int i=;i<n;i++){
scanf("%d", &tmp);
s.insert(tmp);
}
} void work(){
while(true){
std::set<int>::iterator it = s.end();it--;
int tmp = *it;
tmp >>= ;
while (s.find(tmp)!=s.end()){
tmp >>= ;
}
if (tmp){
s.erase(it);
s.insert(tmp);
}else{
break;
}
}
}
void output(){
std::set<int>::iterator it=s.begin();
printf("%d", *it);
for(it++;it!=s.end();it++){
printf(" %d", *it);
}
puts("");
}

codeforces 722D Generating Sets 【优先队列】的更多相关文章

  1. Codeforces 722D. Generating Sets

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

  2. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D. Generating Sets 贪心+优先队列

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

  3. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D. Generating Sets 贪心

    D. Generating Sets 题目连接: http://codeforces.com/contest/722/problem/D Description You are given a set ...

  4. Generating Sets 贪心

    H - Generating Sets Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64 ...

  5. CF722D. Generating Sets[贪心 STL]

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

  6. [codeforces722D]Generating Sets

    [codeforces722D]Generating Sets 试题描述 You are given a set Y of n distinct positive integers y1, y2, . ...

  7. D. Generating Sets 解析(思維)

    Codeforce 722 D. Generating Sets 解析(思維) 今天我們來看看CF722D 題目連結 題目 略,請直接看原題 前言 真的是沒想到... @copyright petje ...

  8. 【53.57%】【codeforces 722D】Generating Sets

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. Codeforces 681C. Heap Operations 优先队列

    C. Heap Operations time limit per test:1 second memory limit per test:256 megabytes input:standard i ...

随机推荐

  1. javascript自定义简单map对象功能

    这里介绍一种js创建简单map对象的方法: function Map() { //创建object对象, 并给object对象添加key和value属性 var obj1=new Object(); ...

  2. js 监听组合键盘事件

    有些时候,我们需要在网页上,增加一些快捷按键,方便用户使用一些常用的操作,比如:保存,撤销,复制.粘贴等等. 我们所熟悉的按键有这么集中类型: 单独的按键操作,如:delete.up.down等 两位 ...

  3. Spring Cloud (6)A config 服务端配置 与github通信

    1. 在git上创建一个库,并复制库地址 2.用git clone项目到本地,并创建application.yml application.yml 内容 --下一步 3.将application.ym ...

  4. C# 类库调试 启动外部程序无法调试

    无法调试进程 test.exe [17936] 中的某些代码.请参阅下面的状态信息. IntelliTrace 代码失败(0x80131534).    Managed (v4.6.v4.5.v4.0 ...

  5. 虚拟机扩容mac

    VMware虚拟机Mac增大容量: 1.设置硬盘容量大小 2.打开虚拟机的终端,找到需要扩展的硬盘.输入命令 :diskutil list 注意 :我的硬盘名字叫yz,这一行可以看见当前分配容量,最后 ...

  6. 开发webapp手机返回键 退出问题 摘录

    mui进行手机物理键的监听 确保引入mui 调用以下函数 // android 返回按键处理 androidBack(store, data) { try { mui.init({ keyEventB ...

  7. UnityHub破解

    1.退出UnityHub,安装好nodejs执行以下命令 npm install -g asar 2.打开UnityHub安装目录如 C:\Program Files\Unity Hub\resour ...

  8. jenkins系列_使用scp命令进行远程文件复制遇到的坑

    转自:https://blog.csdn.net/kingboyworld/article/details/78905553 一.场景介绍 项目为微服务项目,使用jenkins进行统一部署.基本思路是 ...

  9. ReultSet有什么作用和使用

    结果集(ResultSet)是数据中查询结果返回的一种对象,可以说结果集是一个存储查询结果的对象,但是结果集并不仅仅具有存储的功能,他同时还具有操纵数据的功能,可能完成对数据的更新等. int col ...

  10. 用adb取出在手机中安装的apk

    第一步:列出所有安装的apk adb shell pm list packages 然后找到自己要取出来的apk的包名. 第二布:找到apk的位置(后面跟上包名) adb shell pm path ...