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

来源:互联网 发布:淘宝助理安装数字证书 编辑:程序博客网 时间:2024/05/21 06:35

题目链接
D. Generating Sets
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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:

  1. Take any integer xi and multiply it by two, i.e. replace xi with xi.
  2. Take any integer xi, multiply it by two and add one, i.e. replace xi with 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
51 2 3 4 5
output
4 5 2 3 1 
input
615 14 3 13 1 12
output
12 13 14 7 3 1 
input
69 7 13 17 5 11
output
4 5 2 6 3 1 


题意

给出一个数x以及变换,可以变成2x,或者变成2x+1,或者不变,变化可以操作若干次
现在给你n个不同的数Y,你需要找到n个不同的x,使得这n个不同的x经过变化之后,能够得到Y数组,并使得X中数的最大值最小。问你应该怎么做。



题解:
每次取最大的数,然后使最大数变小即可,能变就变,用一个set去维护就好了。

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<vector>#include<set>using namespace std;struct cmp{bool operator()(int x,int y){return x>y;}};int main(){int n;set<int,cmp> cnt;scanf("%d",&n);for(int i=1;i<=n;i++){int p;scanf("%d",&p);cnt.insert(p);} while(1){int p=*cnt.begin();while(p){p/=2;if(cnt.find(p)==cnt.end()&&p!=0){cnt.erase(cnt.begin());cnt.insert(p);break;}}if(p==0) break;}for(int i=1;i<=n;i++){printf("%d ",*cnt.begin());cnt.erase(cnt.begin());}return 0;}

0 0
原创粉丝点击