您的位置:首页 > 运维架构

【codeofrces 722D】【贪心 STL维护】D. Generating Sets 【X集合的数 可以有两种方式变换Y 现在给你一个集合Y 求X 且这个X的里最大的数尽可能小】

2016-10-02 17:12 639 查看
传送门:D. Generating Sets

描述:

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:

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


题意:

X集合的数 可以有两种方式变换Y  现在给你一个集合Y   求X 且这个X的里最大的数 尽可能小

注意原数列和目标数列都必须满足set内元素的性质(即每个元素都不相同),但是在变化的过程中不需要满足这个条件。

思路:
贪心,每次取出最大的数字,除以2,如果没有重复元素的话就结束(也就是说不必一次就把它除到最小)并且放回集合里面,一直这样操作直到最大的数字无法变化为止。这样的贪心思路才是正确的。

代码一:
(堆来维护)

//G++ 327ms 21300k
#include <bits/stdc++.h>
#define pr(x) cout << #x << "= " << x << " " ;
#define pl(x) cout << #x << "= " << x << endl;
#define ll __int64
using namespace std;

template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
const int N=3e5+10;

int y
,n;
map<int,int>mp;
priority_queue<int>q;

int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(0);

read(n);
for(int i=1; i<=n; i++){
read(y[i]);
q.push(y[i]);
mp[y[i]]++;
}
while(!q.empty()){
int tmp=q.top();
int t=tmp;
while(mp[t] && t!=0){
t>>=1;
}
if(t==0)break;
mp[tmp]--;
mp[t]++;
q.pop();
q.push(t);
}
while(!q.empty()){
printf("%d ", q.top());
q.pop();
}
puts("");
return 0;
}
代码二:
(set来维护,用到了set的排序功能)

//G++ 296ms 2800k
#include <bits/stdc++.h>
#define pr(x) cout << #x << "= " << x << " " ;
#define pl(x) cout << #x << "= " << x << endl;
#define ll __int64
using namespace std;

template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
const int N=3e5+10;

int y
,n;
set<int>s;

int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(0);

read(n);
for(int i=1; i<=n; i++){
read(y[i]);
s.insert(y[i]);
}
while(1){
auto it=s.end();//it为末尾下标+1
it--;
int p=*it;
while(s.find(p)!=s.end() && p!=0){//find没找到的话会返回s.end()
p>>=1;
}
if(p==0)break;
s.erase(*it);
s.insert(p);
}
for(auto it=s.begin(); it!=s.end(); it++){
printf("%d ",*it);
}
puts("");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐