time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard 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:

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.

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、除后的数字可能不是这段序列的最大值了。那样的改变是没有意义的。不会再改变整个数列的最大值。

用优先队列维护最大值。每次对最大值进行除2。如果除1次会重复,就一直除。如果除到0了就表示不能进行操作了。则直接返回这个经过处理后的数列。

优先队列可以直接用STL,默认最顶部为最大值。

判断有没有重复则用map;

以下是prioritySTL的用法

#include <algorithm>
#include <queue> using namespace std;
//·····
priority_queue<int> q;

这样做默认为单调递增的队列。即top为最大的元素;

如何改为单调递减的队列我下一篇文章会讲。

一下是完整代码。

#include <cstdio>
#include <algorithm>
#include <queue>
#include <map> using namespace std; priority_queue <int> q;
map <int, int> dic;
int n; int main()
{
//freopen("F:\\rush.txt", "r", stdin);
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
int x;
scanf("%d", &x);
q.push(x);
dic[x] = 1;
}
while (true)
{
int y = q.top();
while (dic[y] && y > 0)
y >>= 1;
if (y == 0)
break;
q.pop();
q.push(y);
dic[y] = 1;
}
bool flag = false;
while (!q.empty())
{
int x = q.top();
if (!flag)
printf("%d", x);
else
printf(" %d", x);
q.pop();
flag = true;
}
printf("\n");
return 0;
}

【53.57%】【codeforces 722D】Generating Sets的更多相关文章

  1. 【53.57%】【codeforces 610C】Harmony Analysis

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

  2. 【 BowWow and the Timetable CodeForces - 1204A 】【思维】

    题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...

  3. 【57.97%】【codeforces Round #380A】Interview with Oleg

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  4. 【34.57%】【codeforces 557D】Vitaly and Cycle

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  5. 【Educational Codeforces Round 53 (Rated for Div. 2) C】Vasya and Robot

    [链接] 我是链接,点我呀:) [题意] [题解] 如果|x|+|y|>n 显然.从(0,0)根本就没法到(x,y) 但|x|+|y|<=n还不一定就能到达(x,y) 注意到,你每走一步路 ...

  6. 【codeforces 750C】New Year and Rating(做法2)

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

  7. 【codeforces 750A】New Year and Hurry

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  8. 【codeforces 750C】New Year and Rating

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

  9. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

随机推荐

  1. 洛谷——P2957 [USACO09OCT]谷仓里的回声Barn Echoes

    https://www.luogu.org/problem/show?pid=2957 题目描述 The cows enjoy mooing at the barn because their moo ...

  2. CODEVS——T 3013 单词背诵

    http://codevs.cn/problem/3013/  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 De ...

  3. [React] Create a queue of Ajax requests with redux-observable and group the results.

    With redux-observable, we have the power of RxJS at our disposal - this means tasks that would other ...

  4. apue和unp的学习之旅07——多种边界条件的讨论

    了解一些边界条件,通过观察这些情形,弄清在网络层次发生什么以及它们怎样反映到套接字api,这将很多其它地理解这些层次的工作原理,体会怎样编写应用程序来处理这些情形. //--------------- ...

  5. 【JEECG技术博文】JEECG 简单实例解说权限控制

    JEECG简单实例解说权限控制 请大家点击这里为我们投票.2015博客之星.很多其他分享敬请期待 博文地址:http://blog.itpub.net/30066956/viewspace-18687 ...

  6. 给Linux设置SSH登录邮件提醒

    给Linux设置SSH登录邮件提醒 心血来潮,用 last 命令查看了登录记录,不看不知道,一看就有问题.竟然有两个陌生的IP ,一个是美国欧莱雅的,一个是北京联通的.真是郁闷,密码简单了真不行 后来 ...

  7. 自定义input[type="checkbox"]样式

    input[type=checkbox] { visibility: hidden; position: relative;} input[type=checkbox]:after { content ...

  8. 网页设计实战3 ufo类型的科技网页如何实现

    网页设计实战3 ufo类型的科技网页如何实现 一.总结 一句话总结:基础的图片素材就是如何几张图片,这个效果只是通过jquery或者js让那个png图片旋转起来了,如此而已.其实核心就是一个trans ...

  9. Jenkins学习总结(1)——Jenkins详细安装与构建部署使用教程

    Jenkins是一个开源软件项目,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能.Jenkins是基于Java开发的一种持续集成工具,用于监控持续重复的工作,功能包括: 1.持续的软件版本发 ...

  10. JS实现动画的四条优化方法

    JS实现动画的四条优化方法 1)如果使用的是setTimeout实现的轮询动画,在每一次执行方法之前需要把前面的设置的定时器清除掉 2)为了防止全局变量的污染,我们把定时器的返回值赋值给当前操作元素的 ...