[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:

  1. Take any integer xi and multiply it by two, i.e. replace xi with 2·xi.
  2. 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的更多相关文章

  1. Codeforces 722D. Generating Sets

    D. Generating Sets time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  2. CF722D. Generating Sets[贪心 STL]

    D. Generating Sets time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  3. 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 ...

  4. Generating Sets 贪心

    H - Generating Sets Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64 ...

  5. 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 ...

  6. D. Generating Sets 解析(思維)

    Codeforce 722 D. Generating Sets 解析(思維) 今天我們來看看CF722D 題目連結 題目 略,請直接看原題 前言 真的是沒想到... @copyright petje ...

  7. codeforces 722D Generating Sets 【优先队列】

    You are given a set Y of n distinct positive integers y1, y2, ..., yn. Set X of n distinct positive ...

  8. 【53.57%】【codeforces 722D】Generating Sets

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. 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 ...

随机推荐

  1. [转]C#综合揭秘——细说多线程(下)

    引言 本文主要从线程的基础用法,CLR线程池当中工作者线程与I/O线程的开发,并行操作PLINQ等多个方面介绍多线程的开发. 其中委托的BeginInvoke方法以及回调函数最为常用. 而 I/O线程 ...

  2. CF982C Cut 'em all!

    思路: 在深搜过程中,贪心地把树划分成若干个连通分支就可以了.划分的条件是某个子树有偶数个节点.注意到在一次划分之后并不需要重新计数,因为一个数加上一个偶数并不影响这个数的奇偶性. 实现: #incl ...

  3. KendoUI Grid Pager部分 Nan-Nan of x Items

    相关问题: http://stackoverflow.com/questions/23941065/pager-error-in-kendo-gridnan-nan-of-1-items http:/ ...

  4. MySQL系列(二)--MySQL存储引擎

    影响数据库性能的因素: 1.硬件环境:CPU.内存.存盘IO.网卡流量等 2.存储引擎的选择 3.数据库参数配置(影响最大) 4.数据库结构设计和SQL语句 MySQL采用插件式存储引擎,可以自行选择 ...

  5. clone对象或数组

    function clone(obj) { var o; if (typeof obj == "object") { if (obj === null) { o = null; } ...

  6. myBatis参数处理 myBatis佟刚课程笔记

    单个参数:myBatis不会做特殊处理 #{参数名}: 取出参数值 多个参数: myBatis会做特殊处理 多个参数会被封装成一个MAP key:param1 param2.... param10,或 ...

  7. 《3+1团队》【Alpha】Scrum meeting 5

    项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 3+1团队 团队博客地址 https://home.cnblogs.com/u/3-1group ...

  8. 阿里云首次安装和部署nginx

    1.执行yum命令安装依赖 yum -y install pcre* yum -y install openssl* 2.下载nginx //如果没有安装wget,下载已编译版本 yum instal ...

  9. 一个电脑安装两个jdk版本

    场景:先前使用了1.6的jdk但是现在学java需要使用jdk1.8的,现在打算电脑上使用两个jdk 1 . 准备两个版本的jdk我的两个jdk路径为: D:\jdk1.7.0_80 D:\Progr ...

  10. Moebius for SQLServer负载均衡

    搞数据库的都知道:在Oracle上有RAC集群,MySQL也有对应的方案,而SQL Server上直到SQL Server 2012版本的AlwaysOn到来,微软都没有提供一个负载均衡方案,在网上看 ...