【codeofrces 722D】【贪心 STL维护】D. Generating Sets 【X集合的数 可以有两种方式变换Y 现在给你一个集合Y 求X 且这个X的里最大的数尽可能小】
来源:互联网 发布:java 写成绩划分 编辑:程序博客网 时间:2024/06/05 08:11
传送门:D. Generating Sets
描述:
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.
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.
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.
51 2 3 4 5
4 5 2 3 1
615 14 3 13 1 12
12 13 14 7 3 1
69 7 13 17 5 11
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 __int64using 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],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 __int64using 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],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;}
- 【codeofrces 722D】【贪心 STL维护】D. Generating Sets 【X集合的数 可以有两种方式变换Y 现在给你一个集合Y 求X 且这个X的里最大的数尽可能小】
- 不可表示的数[x/2] + y + x * y
- BZOJ 1101([POI2007]Zap-满足x<=a&&y<=b&&gcd(x,y)=d的数对个数)
- 求两个整型数的平均值:return (x&y) + ((x^y)>>1)
- 求x的y次方
- 求X,Y的平均值
- Hdu 5812 Distance(三种操作. 1.插入x 2.删除x 3.在集合中找到一个数y,使得y->x,每次能除以一个素数或者乘上一个素数,问最少的操作次数)
- 给你5个数a,b,c,d,k。x属于[a,b]y属于[c,d]。 问你有多少对(x,y)的公约数为k
- scanf("%d%d",&x,&y);与scanf("%d,%d",&x,&y);的区别 c语言输入注意
- 笔试面试13--给定 x, k ,求满足 x + y = x | y 的第 k 小的正整数 y
- 新手喜闻乐见的x++,y++,++x,++y
- Problem D: 求(x-y+z)*2
- Problem D: 求(x-y+z)*2
- 不可表示的数[x/2] + y + x * y(英雄会)
- CSS3的3D转换translate3d(x,y,z)函数
- 最大数max(x,y,z)
- x的y次方
- 【Intel Code Challenge Elimination Round (Div1 + Div2, combined) D】【贪心 暴力 SET】Generating Sets n个不同的x变
- 插入排序(升序)
- C++ strlen()+1问题
- struts2总结
- ac自动机静态比动态快
- dp和px、sp和px的相互转化的工具类
- 【codeofrces 722D】【贪心 STL维护】D. Generating Sets 【X集合的数 可以有两种方式变换Y 现在给你一个集合Y 求X 且这个X的里最大的数尽可能小】
- python中的try/except/else/finally语句
- springmvc 通过aop方式进行登录控制
- FPGA内部pattern生成 仅仅是时序方面
- 操作系统与网络实现 之十六
- fatal error: caffe/blob.hpp: No such file or directory的解决
- 正则表达式的用户名合法性
- BestCoder Roud #88 Find Q
- Problem A. Nearest Neighbor Search ——BNUOJ 点距离立方体最短