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

输入示例

6
9 7 13 17 5 11

输出示例

4 5 2 6 3 1

数据规模及约定

见“输入

题解

建一颗二叉的 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;
}
时间: 2024-12-29 01:07:29

[codeforces722D]Generating Sets的相关文章

Generating Sets 贪心

H - Generating Sets Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description You are given a set Y of ndistinct positive integers y1,?y2,?...,?yn. Set X of ndistinct positive integers x1,?x2,?...,?xn is

CodeForces 722D Generating Sets

贪心,优先队列. 每次变最大的数,变到最大的能变的一个就停止.如果发现最大的数不能变小,那么输出. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<map> #

URAL 1727. Znaika&amp;#39;s Magic Numbers(数学 vector)

主题链接:http://acm.timus.ru/problem.aspx?space=1&num=1727 1727. Znaika's Magic Numbers Time limit: 0.5 second Memory limit: 64 MB Znaika has many interests. For example, now he is investigating the properties of number sets. Znaika writes down some set

挖点坑

想写的题都列在这里吧 bzoj1061: [Noi2008]志愿者招募 bzoj1018: [SHOI2008]堵塞的交通 cf715B.Complete The Graph bzoj1040: [ZJOI2008]骑士 cf722D. Generating Sets cf718C.Sasha and Array

URAL 1727. Znaika&#39;s Magic Numbers(数学 vector)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1727 1727. Znaika's Magic Numbers Time limit: 0.5 second Memory limit: 64 MB Znaika has many interests. For example, now he is investigating the properties of number sets. Znaika writes down some set

Simple Automated Backups for MongoDB Replica Sets

There are a bunch of different methods you can use to back up your MongoDB data, but if you want to avoid downtime and/or potential performance degradation, the most common advice seems to be that you should simply do all your backups on a slave. Thi

The Tree-planting Day and Simple Disjoint Sets

First I have to say: I have poor English. I am too young, too simple, sometimes na?ve. It was tree-planting day two weeks ago. SHENBEN dph taught us a lot about tree-planting and the disjoint sets. It was useful and valuable for a JURUO like me. I ad

Maven在[INFO] Generating project in Interactive mode卡住的问题解决

我的环境: Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T00:41:47+08:00) Maven home: /usr/local/maven3 Java version: 1.8.0_111, vendor: Oracle Corporation Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Ho

Maven &quot;Generating project in Batch mode&quot;问题的解决 (转)

在maven的五分钟入门里面,有这样一个命令: mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false 这句话的命令是创建一个默认的项目,但我在执行这个命令时,命令行会停在 [INFO] Generating project in Batch mode 这句话会停很久