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

来源:互联网 发布:淘宝网在线客服 编辑:程序博客网 时间:2024/05/20 20:46

题目地址:这里
题意:一个数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);    }}
0 0
原创粉丝点击