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 ...
随机推荐
- (转)HTTP 长连接和短连接
1. HTTP协议与TCP/IP协议的关系 HTTP的长连接和短连接本质上是TCP长连接和短连接.HTTP属于应用层协议,在传输层使用TCP协议,在网络层使用IP协议.IP协议主要解决网络路由和寻址问 ...
- querystring 解析url 查询字符串
对前端同学来说,经常要碰到一种比较麻烦的情况,那就是url查询字符串的解析问题.说起来也不难,就是比较麻烦. 具体来处理这种情况的时候,相信有一部分同学就是针对具体项目中的需要的字符去正则匹配一下,业 ...
- echarts 用marlkline画线 同时配置中含有datazoom,怎么设置markline
由于项目需要设置边界值即用markline 画标线,通过echarts文档可以查看到(如下) 1.通过坐标点(xAxis和yAxis的设置) 通过网上搜索许多markline的配置都是通过下面来设置的 ...
- android五种存储方式
http://www.cnblogs.com/smalltigerlee/archive/2011/11/10/2244143.html
- 字符串反转----将this is good 转化为good is this 输出。
思路:现将整个字符串反转,再将每个单词反转: #include "iostream" #include "cstring" using namespace st ...
- paper 120:计算距离矩阵的函数的pdist和pdist2函数
matlab中自带的计算距离矩阵的函数有两个pdist和pdist2.前者计算一个向量自身的距离矩阵,后者计算两个向量之间的距离矩阵.基本调用形式如下: D = pdist(X) D = pdist2 ...
- IP釋放、清除、以及刷新DNS
Windows 10 於桌面按住 Windows + X 按鍵. 選擇 Command Prompt (以管理員執行). 在彈跳視窗中輸入 ipconfig /release. 等待數秒回報此 IP ...
- Python __builtins__模块拾穗
1.isinstance函数:除了以一个类型作为参数,还可以以一个类型元组作为参数. isinstance(obj,basestring)===isinstance(obj,(str,unicode) ...
- VM环境下,快速复制多个SQLServer实例,环境调整
--windows机器名 sysprep.exe 勾选通用,并关机 --实例名 SELECT @@SERVERNAME ,serverproperty('servername') if server ...
- Fiddler 常用文档
时间飞逝,已经俩月有余没写东西了(本来写的就不多,(^▽^)),最近俩月的苦闷,此处省略 1W 字.... 闲言碎语不多讲,使用 Fiddler 有一小段时间了,今天在这里总结下,一来做个笔记,二来可 ...