【Intel Code Challenge Elimination Round (Div1 + Div2, combined) D】【贪心 暴力 SET】Generating Sets n个不同的x变

84 篇文章 5 订阅
55 篇文章 0 订阅
D. Generating Sets
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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:

  1. Take any integer xi and multiply it by two, i.e. replace xi with xi.
  2. Take any integer xi, multiply it by two and add one, i.e. replace xi with 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.

Input

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.

Output

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.

Examples
input
5
1 2 3 4 5
output
4 5 2 3 1 
input
6
15 14 3 13 1 12
output
12 13 14 7 3 1 
input
6
9 7 13 17 5 11
output
4 5 2 6 3 1 

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 50050, M = 0, Z = 1e9 + 7, inf = 0x3f3f3f3f;
template <class T1, class T2>inline void gadd(T1 &a, T2 b) { a = (a + b) % Z; }
int n;
int a[N];
int b[N];
set<int>sot;
int main()
{
	while (~scanf("%d", &n))
	{
		sot.clear();
		for (int i = 1; i <= n; ++i)
		{
			int x; scanf("%d", &x);
			sot.insert(x);
		}
		while (1)
		{
			int x = *--sot.end();

			int y = x;
			while (y)
			{
				y = y / 2;
				if (!sot.count(y))break;
			}
			if (y == 0)break;
			sot.erase(x);
			sot.insert(y);
		}
		for (auto x : sot)printf("%d ", x);
		puts("");
	}
	return 0;
}
/*
【题意】
给定n个不同y[]
让你输出一个x[],使得x[]两两不同,且x[]可以经过若干次*2或*2+1操作变为y[]
你构造的的x[]中,最大的x[i]要尽可能小

【类型】
贪心 暴力

【分析】
x变为y可能面临*2还是*2+1
但是y变为x只能/2
所以我们直接考虑把y变为x
显然只要把最大的y往小了缩就可以了。

一旦缩小到怎么缩小都会和其他数相同,说明有一个数字串已经从1 1x 1xx 1xxx 1xxxx 1xxxxx 都具备
也就是说,不光这个数无法缩小,这个数暂时不动,其他数缩小,也不会给他空出缩小的空间。
于是这个时候就可以停止操作,输出序列

【时间复杂度&&优化】
O(nlogn)

*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值