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. MonGoDB 常见操作, 设置管理员和用户登入

    [ 启动客户端 => ./bin/mongo --host 192.168.200.100 ] 1: 查看所有已经创建的数据库  =>  show dbs   2: 切换或者创建数据库   ...

  2. java json字符串与对象转换

    下载引入包gson-2.0.jar 1.字符转数据 final Map map = new HashMap();map.put("contents",    "[{\&q ...

  3. leetcode212

    class Solution { public List<String> findWords(char[][] board, String[] words) { List<Strin ...

  4. 机器学习进阶-疲劳检测(眨眼检测) 1.dist.eculidean(计算两个点的欧式距离) 2.dlib.get_frontal_face_detector(脸部位置检测器) 3.dlib.shape_predictor(脸部特征位置检测器) 4.Orderdict(构造有序的字典)

    1.dist.eculidean(A, B) # 求出A和B点的欧式距离 参数说明:A,B表示位置信息 2.dlib.get_frontal_face_detector()表示脸部位置检测器 3.dl ...

  5. js 迭代 方法

    在js 中,有一些方法, 可以很方便的遍历出数据,不用进行for循环就可以,是怎么实现的呢? 例如:在javascript 中的forEach 方法:打印一个数组的索引和项目: 1. forEach  ...

  6. echarts.js制作中国地图

    一.准备 1.  打开sublime,新建一个echarts文件夹,新建echarts.html文件 2.  在echarts.html文件中,为ECharts准备一个Dom(id是china-map ...

  7. 中文转码器的工作原理_delphi教程

    最近在做Delphi下的简体与繁体转换, 发现Windows2000自带的工具"中文转码器"很好用, 不仅可以转内码(BIG5-->GBK), 还可以将繁体字转为简体字(如: ...

  8. numpy-帮助文档 & 速查表.md

    目录 转相关资料: 速查表 速查代码 转相关资料: 官方手册 易佰教程 gitbook ZMonster's Blog 速查表 速查代码 # -*- coding: utf-8 -*- "& ...

  9. Structs复习 命名空间

    引入jar包 web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version= ...

  10. Mysql字段类型与合理选择

    字段类型 数值 MySQL 的数值数据类型可以大致划分为两个类别,一个是整数,另一个是浮点数或小数.许多不同的子类型对这些类别中的每一个都是可用的,每个子类型支持不同大小的数据,并且 MySQL 允许 ...