Generating Sets 贪心
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
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:
- 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.
Sample Input
5
1 2 3 4 5
4 5 2 3 1
6
15 14 3 13 1 12
12 13 14 7 3 1
6
9 7 13 17 5 11
4 5 2 6 3 1
//优先队列+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 贪心的更多相关文章
- CF722D. Generating Sets[贪心 STL]
D. Generating Sets time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 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 ...
- 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 ...
- Codeforces 722D. Generating Sets
D. Generating Sets time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- [codeforces722D]Generating Sets
[codeforces722D]Generating Sets 试题描述 You are given a set Y of n distinct positive integers y1, y2, . ...
- D. Generating Sets 解析(思維)
Codeforce 722 D. Generating Sets 解析(思維) 今天我們來看看CF722D 題目連結 題目 略,請直接看原題 前言 真的是沒想到... @copyright petje ...
- CodeForces 1042 F Leaf Sets 贪心
Leaf Sets 题意:给你一棵树,树上有n个点,只有一条边的点叫做叶子,现在要求把所有的叶子分组,每个组内的所有叶子的距离都不能大于k. 题解: 我们可以随意找一个不是叶子的节点当做这颗树的根节点 ...
- codeforces 722D Generating Sets 【优先队列】
You are given a set Y of n distinct positive integers y1, y2, ..., yn. Set X of n distinct positive ...
- cf1042F. Leaf Sets(贪心)
题意 题目链接 给出一棵树,删除一些边,使得任意联通块内的任意点距离不超过$k$ sol 考场上想的贪心是对的:考虑一棵子树,如果该子树内最深的两个节点的距离相加$>k$就删掉最深的那个点,向上 ...
随机推荐
- lock+Condition
关键字 synchronized+wait/notify/notifyAll可以实现等待/通知模式,类ReentrantLock可以实现同样的功能,但需要借助Condition对象.Condition ...
- opencv-学习笔记(6)图像梯度Sobel以及canny边缘检测
opencv-学习笔记(6)图像梯度Sobel以及canny边缘检测 这章讲了 sobel算子 scharr算子 Laplacion拉普拉斯算子 图像深度问题 Canny检测 图像梯度 sobel算子 ...
- C#通过gridview导出excel
[CustomAuthorize] public FileResult ExportQuestionCenterExcel(SearchBaseQuestion search) ...
- Linux error:No space left on device
一台Oracle数据库服务器在关机重启后,Oracle监听无法启动,提示错误 Linux error:no space left on device 提示可知:问题是出在磁盘空间不足 但是初步查看分区 ...
- 将HTML页面页脚固定在页面底部(多种方法实现)
当一个HTML页面中含有较少的内容时,Web页面的footer部分随着飘上来,处在页面的半腰中间,给视觉效果带来极大的影响,接下来为大家介绍下如何将页脚固定在页面底部,感兴趣的朋友可以了解下 作为一个 ...
- Thunder团队Beta周贡献分分配结果
小组名称:Thunder 项目名称:爱阅app 组长:王航 成员:李传康.翟宇豪.邹双黛.苗威.宋雨.胡佑蓉.杨梓瑞 分配规则 规则1:基础分,拿出总分的20%(8分)进行均分,剩下的80%(32分) ...
- object-oriented 第二次作业(2)
面向对象程序设计自学计划 由于我的英文实在是很差,所以我就没有去考虑看英文的课程视频.网络上的课程有很多,什么学校的也有,一开始我不知道该如何开始选择课程.感觉每个都还可以.后来在群里的看到别人推荐的 ...
- 第一、二章——Python简介与Python基础
前言:<Data Wrangling with Python>这本书主要是讲使用Pyhon来处理各种类型保存的数据的. 第一章:Python简介 1.版本选择 本书选择的Python版本是 ...
- iOS开发libz.dylib介绍
libz.dylib这个Xcode系统库文件经常用到.这个其实是个动态链接库. 后缀名为.dylib的文件是一个动态库,这个库是运行时加载而不是编译时加载.这个也说明了obj-C是运行时语言,也就是数 ...
- Spyder5 & 显示器校准 & 色彩校准
Spyder5 & 显示器校准 & 色彩校准 Spyder5EXPRESS 绿蜘蛛5 – 轻松.快速地校准您的屏幕. Spyder5PRO 蓝蜘蛛5 – 可为您的所有笔记本电脑和台式机 ...