(优先队列+贪心)codeforces - 722D Generating Sets

原题链接:http://codeforces.com/problemset/problem/722/D 


 

题意:

数列Y,每个数都不同,这是数列是由数列X生成的。X中每个数都不同,并且生成方法有三种

1、保持原来的

2、x替换成x*2

3、x替换成x*2+1

需要求使X中最大数尽量小的数列X。


 

分析:

天哪,想到了用map保存是否存在,这么lowbee的题居然没想到优先队列。。唉,好心酸。

写了几十行的递归,还WA,卧槽。。。

这题很简单,只要不断的让最大值尽量往小的变就行了。


 

代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cmath>
 5 #include<cstring>
 6 #include<set>
 7 #include<vector>
 8 #include<queue>
 9 #include<map>
10 #include<list>
11 #include<bitset>
12 #include<string>
13 #include<cctype>
14 #include<cstdlib>
15 #include<sstream>
16 
17 using namespace std;
18 
19 typedef long long ll;
20 typedef unsigned long long ull;
21 #define inf (0x3f3f3f3f)
22 #define lnf (0x3f3f3f3f3f3f3f3f)
23 #define eps (1e-8)
24 int sgn(double a) {
25     return a < -eps ? -1 : a < eps ? 0 : 1;
26 }
27 
28 
29 const int maxn=50010;
30 int a[maxn];
31 map<int,bool> m;
32 priority_queue<int> pq;
33 
34 
35 void solve() {
36     int n;
37     scanf("%d",&n);
38 
39     for(int i=1; i<=n; i++) {
40         scanf("%d",&a[i]);
41         pq.push(a[i]);
42         m[a[i]]=true;
43     }
44     while(!pq.empty()) {
45         int s=pq.top();
46         int tmp=s;
47         while(m[tmp]&&tmp!=0) {
48             tmp/=2;
49         }
50         if(tmp==0)break;
51         m[s]=false;
52         m[tmp]=true;
53         pq.pop();
54         pq.push(tmp);
55     }
56     while(!pq.empty()) {
57         printf("%d ",pq.top());
58         pq.pop();
59     }
60 }
61 
62 
63 
64 int main() {
65 
66 #ifndef ONLINE_JUDGE
67     freopen("in.txt", "r", stdin);
68     //freopen("out.txt", "w", stdout);
69 #endif
70     //iostream::sync_with_stdio(false);
71     solve();
72     return 0;
73 }

 

posted @ 2016-10-17 20:27  tak_fate  阅读(322)  评论(0编辑  收藏  举报