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 ...
随机推荐
- iOS,视图相关
1.移除视图的所以子视图 2.自定义视图(UIView) 3.处理悬浮窗口(类似微信视频),等比缩放 4.自定义前面视图(可以手写字) 5.图片拉伸的几种方式,计算文本占用空间大小 6.UILable ...
- PC管理端与评委云打分配合步骤及疑难问题汇编,即如何使用PC管理端的云服务管理功能
一.前期环境及数据准备 A.PC管理端主要流程 1.进入菜单 编辑/选项/服务器 界面,如下图所示,采用我官方所提供的云服务,不采用自己假设的AppWeb服务. 切记:AppWeb服务和云服务只能二选 ...
- CSS生成内容
在Web中插入内容,在CSS2.1时代依靠的是JavaScript来实现.但进入CSS3进代之后我们可以通过CSS3的伪类“:before”,“:after”和CSS3的伪元素“::before”.“ ...
- getRemoteAddr()和getRemoteHost() 区别
System.out.println("request.getRemoteAddr(): " + request.getRemoteAddr()); System.out.prin ...
- Android系统调用
android 中intent是经常要用到的.不管是页面牵转,还是传递数据,或是调用外部程序,系统功能都要用到intent. 在做了一些intent的例子之后,整理了一下intent,希望对大家有用. ...
- 解决jquery mobile的header和footer在点击屏幕的时候消失的办法
给header和footer添加 data-position="fixed" 和 data-tap-toggle="false"即可,代码如下: <div ...
- 【002:ESP8266 移植 Mqtt 】
系统环境: ubuntu 16.04 64bitSDK版本: esp_iot_sdk_v1.3.0记录时间: 2016年10月13日联系方式: yexiaopeng1992@126.com 背景:在E ...
- iphone H5视频行内播放(禁止全屏播放)
一般用户都知道,ios在网页点击视频播放时,视频会弹出全屏播放框. video标签的playsinline.webkit-playsinline标记根本就不会起作用. 还有传闻说对于没有声音的视频不会 ...
- Hadoop namenode无法启动
最近遇到了一个问题,执行start-all.sh的时候发现JPS一下namenode没有启动 每次开机都得重新格式化一下namenode才可以 其实问题就出在tmp文件,默 ...
- vs加js引用
今天又有一个同事问我“在VS中如何让一个JS文件智能提示另一个JS文件中的成员”,他说Google了一下,并没有找到答案,然后我把这个小技巧贴出来,希望能被Google到. 有时候会有这种情况:当我的 ...