H - Generating Sets

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Submit Status

Description

You are given a set Y of ndistinct positive integers y1, y2, ..., yn.

Set X of ndistinct 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.

Sample Input

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 
 题意:
n个数的数组y[1~n],每个数各不相同,求一个每个数各不相同的x数组x[1~n],使得x中的数经过若干次两种操作变成y数组。操作:x[i]=2*x[i],x[i]=2*x[i]+1;
求最大值最小的一个x数组。
代码:
//优先队列+map,将y数组存入优先队,用map标记每个y[i]是否在优先队列中,
//每次取最大的一个y[i],看队列中有没有y[i]/2,没有就加入y[i]/2,除去y[i],
//如果有再看y[i]/2/2有没有.....直到除到1,队列中还有1就说明不能再减小了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<map>
using namespace std;
int a[];
map<int,int>mp;
struct cmp{
bool operator () (int &a,int &b){
return a<b;
}
};
int main()
{
int n,x;
scanf("%d",&n);
priority_queue<int,vector<int>,cmp>q;
for(int i=;i<n;i++){
scanf("%d",&x);
mp[x]=;
q.push(x);
}
mp[]=;
while(){
int x=q.top();
while(x>){
if(mp[x/]) x/=;
else{
mp[x/]=;
q.push(x/);q.pop();
break;
}
}
if(x==) break;
}
printf("%d",q.top());q.pop();
while(!q.empty()){
printf(" %d",q.top());
q.pop();
}
printf("\n");
return ;
}

Generating Sets 贪心的更多相关文章

  1. CF722D. Generating Sets[贪心 STL]

    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 题目连接: http://codeforces.com/contest/722/problem/D Description You are given a set ...

  3. 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 ...

  4. Codeforces 722D. Generating Sets

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

  5. [codeforces722D]Generating Sets

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

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

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

  7. CodeForces 1042 F Leaf Sets 贪心

    Leaf Sets 题意:给你一棵树,树上有n个点,只有一条边的点叫做叶子,现在要求把所有的叶子分组,每个组内的所有叶子的距离都不能大于k. 题解: 我们可以随意找一个不是叶子的节点当做这颗树的根节点 ...

  8. codeforces 722D Generating Sets 【优先队列】

    You are given a set Y of n distinct positive integers y1, y2, ..., yn. Set X of n distinct positive ...

  9. cf1042F. Leaf Sets(贪心)

    题意 题目链接 给出一棵树,删除一些边,使得任意联通块内的任意点距离不超过$k$ sol 考场上想的贪心是对的:考虑一棵子树,如果该子树内最深的两个节点的距离相加$>k$就删掉最深的那个点,向上 ...

随机推荐

  1. vmware安装64位系统“此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态”的问题

    虚拟机使用的是VMware Workstation,并且首次在虚拟机体验64 位系统.在新建好虚拟机,运行时候就出现了VMware Workstation 的提醒:此主机支持 Intel VT-x,但 ...

  2. Python运行的方式

    Python的运行方式多种多样,下面列举几种: 交互式 在命令行中输入python,然后在>>>提示符后面输入Python语句,这里需要注意: 1 语句前面不能有空格,否则会报错 2 ...

  3. C语言文件进阶操作

    Description文件a.dic.b.dic.c.dic中分别存有张三的三科成绩,每个文件都是16字节:前8个字节存储其英文名字zhangsan,后面是一个空格,其后的2个字节存储其年龄(文本方式 ...

  4. android AndroidManifest.xml uses-feature 详解

    如果你是一个Android用户,而且你有一个老旧的安装有android 1.5 的android设备,你可 能会注意到一些高版本的应用没有在手机上的Android Market 中显示.这必定是应用使 ...

  5. 如何:调试 .NET Framework 源代码

    文章标题:如何:调试 .NET Framework 源代码 文章地址:https://technet.microsoft.com/zh-cn/cc667410.aspx

  6. 浏览器中event.srcElement和event.target的兼容性问题

    在IE下,event对象有srcElement属性,但是没有target属性:Firefox下,even对象有target属性,但是没有srcElement属性.. 解决方法:使用obj(obj = ...

  7. 团队作业7——第二次项目冲刺-Beta版本项目计划

    上一个阶段的总结: 在Alpha阶段,我们小组已近完成了大部分的功能要求,小组的每一个成员都发挥了自己的用处.经过了这么久的磨合,小组的成员之间越来越默契,相信在接下来的合作中,我们的开发速度会越来越 ...

  8. Debian常用設置

    1. 更新軟件源 sudo cp /etc/apt/sources.list /etc/apt/sources.list_bak #備份 sudo vi /etc/apt/sources.list / ...

  9. Filezilla 绿色版 禁止升级 能用。

    FileZilla还是挺好用的,但是如果钟情于 绿色版的话,肯定首选是 免安装绿色版.但是呢,能找到的所谓的免升级 绿色版,都不能用.只要是打开软件了,就会在你还没有设置更新之前,就已经升级号了.并且 ...

  10. getGeneratedKeys自动获取主键的方法

    public class Demo { public static void main(String[] args) { try { String sql="insert into pers ...