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 ...
随机推荐
- 查看https是否支持ATS
nscurl --ats-diagnostics --verbose https://我的域名
- linux/unix 编程手册 fork()函数
父进程通过fork()函数创建子进程,将父进程数据段和栈的内容拷贝到子进程中,子进程执行程序execve创建新程序,调用exit函数退出到等待wait(),挂起父进程, 父子进程享用相同的程序文本段. ...
- Android Animation(动画)
前言 Android 平台提供实现动画的解决方案(三种) 一.3.0以前,android支持两种动画: (1)Frame Animation:顺序播放事先做好的图像,与gif图片原理类似,是一种逐帧动 ...
- debian的版本演进
debian有三个发行版本,stable版.testing版和unstable版. 其中,unstable版本是开发者正在开发的版本,它里面保存着debian的开发者当前的工作.这个版本是不断在被更新 ...
- python学习笔记系列----(三)数据结构
这一章主要是介绍了python一些常用的数据结构,比如list,tuple,dictionary,set,以及一些实用遍历技巧,主要讲的还是list. 3.1 list 主要介绍了list的一些常用的 ...
- MATLAB的GUI
% 常使用的对象查看和设置函数 % .get.set函数 ) % 获得句柄值为0的对象的属性,即显示器对象属性 plot([:]); % 绘制一幅图 title('示例'); % 增加text对象 % ...
- [原创] Gradle DSL method not found: 'android()' 和 buildToolsVersion is not specified 的解决办法。
今天在用Android Studio 2.0 打开别人的较早版本生成的工程时, 提示: Gradle DSL method not found: 'android()'. 解决办法为,打开根目录下面的 ...
- 初学软件测试之——如何使用Junit4
前几天刚刚接触软件测试,这篇文章主要是介绍使用eclipse的插件——Junit4进行单元测试.下面先介绍一下单元测试的定义:单元测试(unit testing),是指对软件中的最小可测试单元进行检查 ...
- Web Word和Excel
暂时收集点资料备用 Excel http://www.cnblogs.com/downmoon/archive/2011/05/30/2063258.html http://www.cnblogs.c ...
- 【单点登录】【两种单点登录类型:SSO/CAS、相同一级域名的SSO】
单点登录:SSO(Single Sign On) 什么是单点登录:大白话就是多个网站共享一个用户名和密码的技术,对于普通用户来说,只需要登录其中任意一个网站,登录其他网站的时候就能够自动登陆,不需要再 ...