Codeforces 722D. Generating Sets
2 seconds
256 megabytes
standard input
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.
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.
5
1 2 3 4 5
4 5 2 3 1
6
15 14 3 13 1 12
12 13 14 7 3 1
6
9 7 13 17 5 11
4 5 2 6 3 1
题目大意:给定一个集合Y,集合Y有一些1e9以内的正整数组成,求一个集合X,拥有和集合Y一多的元素,每一个在X集合中的元素可以*2或者*2+1,且进行任意次操作之后变为Y集合,求一种X集合使得X集合中的最大值最小
sol:显然贪心具有正确性,我们每次选取一个最大的数字,看它/2之后是否冲突,如果冲突就再/2,一直到不冲突,若一直冲突就结束。
用队来维护,复杂度O(n^(logn*logx,i))
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<cmath>
#include<ctime>
#include<cstring>
#include<queue>
#include<set>
#define yyj(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout);
#define llg long long
#define maxn 200010
using namespace std;
llg i,j,k,n,m,x,a[maxn]; struct node
{
long long val;
bool operator <(const node &rhs) const
{
return val < rhs.val;
}
}; priority_queue<node> q;
set <node> s; int main()
{
// yyj("d");
cin>>n;
node qq;
for (i=;i<=n;i++)
{
scanf("%I64d",&x);
qq.val=x;
s.insert(qq);
q.push(qq);
}
node w;
while ()
{
qq=q.top();
w.val=qq.val/;
if (w.val==) break;
while (s.find(w)!=s.end() && w.val/!=) w.val/=;
while (s.find(w)==s.end())
{
s.erase(qq);
s.insert(w);
q.pop();
q.push(w);
qq=q.top(); w.val=qq.val/;
if (w.val==) break;
while (s.find(w)!=s.end() && w.val/!=) w.val/=;
}
break;
}
while (!q.empty())
{
cout<<q.top().val<<" ";
q.pop();
}
return ;
}
Codeforces 722D. Generating Sets的更多相关文章
- codeforces 722D Generating Sets 【优先队列】
You are given a set Y of n distinct positive integers y1, y2, ..., yn. Set X of n distinct positive ...
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D. Generating Sets 贪心
D. Generating Sets 题目连接: http://codeforces.com/contest/722/problem/D Description You are given a set ...
- CF722D. Generating Sets[贪心 STL]
D. Generating Sets time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Generating Sets 贪心
H - Generating Sets Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64 ...
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D. Generating Sets 贪心+优先队列
D. Generating Sets time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- [codeforces722D]Generating Sets
[codeforces722D]Generating Sets 试题描述 You are given a set Y of n distinct positive integers y1, y2, . ...
- D. Generating Sets 解析(思維)
Codeforce 722 D. Generating Sets 解析(思維) 今天我們來看看CF722D 題目連結 題目 略,請直接看原題 前言 真的是沒想到... @copyright petje ...
- 【53.57%】【codeforces 722D】Generating Sets
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Codeforces 486D Valid Sets:Tree dp【n遍O(n)的dp】
题目链接:http://codeforces.com/problemset/problem/486/D 题意: 给你一棵树,n个节点,每个节点的点权为a[i]. 问你有多少个连通子图,使得子图中的ma ...
随机推荐
- dubbo源码分析6-telnet方式的管理实现
dubbo源码分析1-reference bean创建 dubbo源码分析2-reference bean发起服务方法调用 dubbo源码分析3-service bean的创建与发布 dubbo源码分 ...
- Pointcut is malformed: Pointcut is not well-formed: expecting 'identifier' at character position 0 ^
错误提示: 解决方法:指定execution 在执行目标方法之前指定execution 例如: import org.aspectj.lang.annotation.Aspect; import or ...
- position
1.父集设置了relative,子集的absolute要设置top和left的值,不然父集的padding值会影响到子集的位置: #first{ width: 200px; height: 100px ...
- 编辑word文档过程中输入法无法正常使用
编辑word文档过程中输入法无法正常使用怎么办??有的朋友在使用Word 2010过程中,遇到了这样的问题.每次打开word文档,程序就自动变成英文输入法,中文输入法就退出了,特别是搜狗输入法.即使在 ...
- Vs2012编写C语言
本来我也是用VC++6.0编写C语言的,但是由于这个版本过老并且和win7,win8,win10的不兼容所以去下载了vs2012,一开始的时候不知道怎么用,现在学会了就写一个教程分享一下. 这时就遇到 ...
- 利用ANTLR4实现一个简单的四则运算计算器
利用ANTLR4实现一个简单的四则运算计算器 ANTLR4介绍 ANTLR能够自动地帮助你完成词法分析和语法分析的工作, 免去了手写去写词法分析器和语法分析器的麻烦 它是基于LL(k)的, 以递归下降 ...
- python中的argparse模块学习
该模块是python用于解析命令行和参数的标准模块 好文推荐:http://blog.ixxoo.me/argparse.html,不仅域名个性,文章翻译的也很好.推荐
- Nop源码分析三
程序的初始化工作和Ioc工作已经做完,nop默认引擎已经初始化. 下面在回到global文件的启动方法Application_Start()中, 1,继续分析下面的代码: var dependency ...
- python学习笔记之装饰器、递归、算法(第四天)
参考老师的博客: 金角:http://www.cnblogs.com/alex3714/articles/5161349.html 银角:http://www.cnblogs.com/wupeiqi/ ...
- 【转】mysql对large page的支持
昨天同事问我关于大页内存的事,我也只是有个模糊的概念,从别的博客转过来的,先记录下 在 Linux 操作系统上运行内存需求量较大的应用程序时,由于其采用的默认页面大小为 4KB,因而将会产生较多 TL ...