您的位置:首页 > 其它

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

2017-03-19 20:42 555 查看
题目地址:这里

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

解法:贪心,每次选择最大的数,然后使得最大数变小即可,能变就变,用一个set去维护就好了。

//CF 722D

#include <bits/stdc++.h>
using namespace std;
set <int> s;
int n;
int main()
{
scanf("%d", &n);
for(int i = 1; i <= n; i++){
int x;
scanf("%d", &x);
s.insert(-x);
}
while(1){
int x = -*s.begin();
int k = *s.begin();
x /= 2;
while(x){
if(s.find(-x) == s.end()){
s.insert(-x);
break;
}
x/=2;
}
if(x == 0){
for(auto it : s){
cout << -it << " ";
}
cout << endl;
return 0;
}
s.erase(k);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐