[codeforces722D]Generating Sets
[codeforces722D]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.
输入示例
输出示例
数据规模及约定
见“输入”
题解
建一颗二叉的 Trie 树,把每个数转化成二进制并映射到树上,然后每次找到最大的那个数往根节点上跳:一步步向上走,一遇到没有被占用的点就停下来,定居在这里;如果一直到根节点都是被别人占用的点,就说明不可能再把最大值减小了,输出并退出程序即可。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std; const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
if(Head == Tail) {
int l = fread(buffer, 1, BufferSize, stdin);
Tail = (Head = buffer) + l;
}
return *Head++;
}
int read() {
int x = 0, f = 1; char c = Getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = Getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = Getchar(); }
return x * f;
} #define maxn 50010
#define maxnode 1600010
#define maxlog 32 int rt, ToT, fa[maxnode], ch[maxnode][2], node[maxn], id2[maxlog];
bool tag[maxnode];
void build(int& o, int v) {
int num[maxlog], cnt = 0;
while(v) num[++cnt] = v & 1, v >>= 1;
// for(int i = cnt; i; i--) putchar(num[i] + '0'); putchar('\n');
if(!o) o = rt;
for(int i = cnt - 1; i; i--) {
int& to = ch[o][num[i]];
if(!to) to = ++ToT, fa[to] = o;
o = to;
}
tag[o] = 1;
return ;
}
int Move(int& o) {
int u = o;
while(fa[u] && tag[u]) u = fa[u];
if(u && !tag[u]) tag[o] = 0, tag[u] = 1, o = u;
int res = 1; u = o;
int num[maxlog], cnt = 0;
while(fa[u]) num[++cnt] = (ch[fa[u]][1] == u), u = fa[u];
for(int i = cnt; i; i--) res = res << 1 | num[i];
return res;
} struct Num {
int v, id;
Num() {}
Num(int _, int __): v(_), id(__) {}
bool operator < (const Num& t) const { return v < t.v; }
} ;
priority_queue <Num> Q;
int ans[maxn], cnta; int main() {
id2[0] = 1;
for(int i = 1; i < maxlog; i++) id2[i] = id2[i-1] << 1;
rt = ToT = 1;
int n = read();
for(int i = 1; i <= n; i++) {
int x = read(); Q.push(Num(x, i));
build(node[i], x);
}
/*for(int i = 1; i <= ToT; i++) printf("%d: %d(%d %d)[%d]\n", i, fa[i], ch[i][0], ch[i][1], tag[i]);
for(int i = 1; i <= n; i++) printf("%d%c", node[i], i < n ? ' ': '\n');*/ while(1) {
Num u = Q.top(); Q.pop();
int tmp = Move(node[u.id]);
// printf("%d -> %d(%d)\n", u.v, tmp, node[u.id]);
Q.push(Num(tmp, u.id));
if(tmp == u.v) break;
} while(!Q.empty()) ans[++cnta] = Q.top().v, Q.pop();
for(int i = 1; i <= cnta; i++) printf("%d%c", ans[i], i < cnta ? ' ' : '\n'); return 0;
}
[codeforces722D]Generating Sets的更多相关文章
- Codeforces 722D. Generating Sets
D. Generating Sets time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- CF722D. Generating Sets[贪心 STL]
D. Generating Sets time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 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 ...
- 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 ...
- D. Generating Sets 解析(思維)
Codeforce 722 D. Generating Sets 解析(思維) 今天我們來看看CF722D 題目連結 題目 略,請直接看原題 前言 真的是沒想到... @copyright petje ...
- codeforces 722D Generating Sets 【优先队列】
You are given a set Y of n distinct positive integers y1, y2, ..., yn. Set X of n distinct positive ...
- 【53.57%】【codeforces 722D】Generating Sets
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) A B C D 水 模拟 并查集 优先队列
A. Broken Clock time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
随机推荐
- CentOS 6.9:MySQL Daemon failed to start.
[root@Server_1 12:02:58 ~ 25]#service mysqld start MySQL Daemon failed to start.Starting mysqld: [ro ...
- sql注入原理及解决方案
sql注入原理 sql注入原理就是用户输入动态的构造了意外sql语句,造成了意外结果,是攻击者有机可乘 SQL注入攻击指的是通过构建特殊的输入作为参数传入Web应用程序,而这些输入大都是SQL语法里的 ...
- 简要记录下localStorage在项目中的应用之一
localStorage作为HTML5本地存储web storage特性的API之一,主要作用是将数据保存在客户端中.localStorage保存的数据,一般情况下是永久保存的,也就是说只要采用loc ...
- 一个Java编写的小玩意儿--脚本语言解释器(一)
今天开始想写一个脚本语言编译器.在这个领域,我还是知道的太少了,写的这个过程肯定是艰辛的,因为之前从来没有接触过这类的东西.写在自己的博客里,算是记录自己的学习历程吧.相信将来自己有幸再回过头来看到自 ...
- FPGA内部RAM的初始化
Altera的RAM初始化文件格式是mif和hex. QuartusII自带的RAM初始化工具很方便产生初始化文件. Xilinx的RAM初始化文件格式是coe, 在vivado中软件会将coe文件变 ...
- postman做压力测试
压力测试 当你需要验证你的接口的抗压能力的时候,可以点击Runner,进行压力测试 注意:压力测试只能以文件夹的方式执行多个接口,不能单独执行,如果想要测试某一个接口,就创一个文件夹,这个文件夹里只有 ...
- Mac 查看端口情况
一个进程可以占用多个端口. 查看某个进程占用哪些端口: lsof -nP | grep TCP | grep -i 进程名 ➜ cocos_creator lsof -nP | grep TCP | ...
- centos7下LVM挂载和扩容
说明:此操作在centos7下进行,如果是centos6发行版,需要注意格式化LV的文件系统类型(centos7.0开始默认文件系统是xfs,centos6是ext4).最后一步写入系统的类型,其中文 ...
- mongodb windows 开机启动
1)新建存放db E:\mongodb\data\db 2)新建日志文件 E:\mongodb\logs\log.txt 3)管理员运行Cmd 4)CD mongodb安装路径 5)运行命令 mong ...
- Python打包成exe,pyc
D:\mypython\path\ C:\Python27\Scripts\pyinstaller.exe -w mypython.py # Python打包成exe D:\mypython\path ...