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

本文主要是介绍【Intel Code Challenge Elimination Round (Div1 + Div2, combined) D】【贪心 暴力 SET】Generating Sets n个不同的x变,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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)*/


这篇关于【Intel Code Challenge Elimination Round (Div1 + Div2, combined) D】【贪心 暴力 SET】Generating Sets n个不同的x变的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/610332

相关文章

气象站的种类和应用范围可以根据不同的分类标准进行详细的划分和描述

气象站的种类和应用范围可以根据不同的分类标准进行详细的划分和描述。以下是从不同角度对气象站的种类和应用范围的介绍: 一、气象站的种类 根据用途和安装环境分类: 农业气象站:专为农业生产服务,监测土壤温度、湿度等参数,为农业生产提供科学依据。交通气象站:用于公路、铁路、机场等交通场所的气象监测,提供实时气象数据以支持交通运营和调度。林业气象站:监测林区风速、湿度、温度等气象要素,为林区保护和

[分布式网络通讯框架]----Zookeeper客户端基本操作----ls、get、create、set、delete

Zookeeper数据结构 zk客户端常用命令 进入客户端 在bin目录下输入./zkCli.sh 查看根目录下数据ls / 注意:要查看哪一个节点,必须把路径写全 查看节点数据信息 get /第一行代码数据,没有的话表示没有数据 创建节点create /sl 20 /sl为节点的路径,20为节点的数据 注意,不能跨越创建,也就是说,创建sl2的时候,必须确保sl

Java零基础-集合:Set

哈喽,各位小伙伴们,你们好呀,我是喵手。运营社区:C站/掘金/腾讯云;欢迎大家常来逛逛   今天我要给大家分享一些自己日常学习到的一些知识点,并以文字的形式跟大家一起交流,互相学习,一个人虽可以走的更快,但一群人可以走的更远。   我是一名后端开发爱好者,工作日常接触到最多的就是Java语言啦,所以我都尽量抽业余时间把自己所学到所会的,通过文章的形式进行输出,希望以这种方式帮助到更多的初

SpringBoot中如何监听两个不同源的RabbitMQ消息队列

spring-boot如何配置监听两个不同的RabbitMQ 由于前段时间在公司开发过程中碰到了一个问题,需要同时监听两个不同的rabbitMq,但是之前没有同时监听两个RabbitMq的情况,因此在同事的帮助下,成功实现了监听多个MQ。下面我给大家一步一步讲解下,也为自己做个笔记; 详细步骤: 1. application.properties 文件配置: u.rabbitmq.ad

代码随想录算法训练营第三十九天|62.不同路径 63. 不同路径 II 343.整数拆分 96.不同的二叉搜索树

LeetCode 62.不同路径 题目链接:62.不同路径 踩坑:二维的vector数组需要初始化,否则会报错访问空指针 思路: 确定动态数组的含义:dp[i][j]:到达(i,j)有多少条路经递推公式:dp[i][j] = dp[i-1][j] + dp[i][j-1]初始化动态数组:dp[0][0] = 1遍历顺序:从左到右,从上到下 代码: class Solution {pu

sqlplus,set linesize ,set pagesize,

http://news.newhua.com/news1/program_database/2010/35/103515315648FH5964A3880I1AFJ6AADGE596E1HFE0375AJKHC4F41.html   Oracle里的set零零碎碎的,这里整理归纳一下 SQL> set timing on;          //设置显示“已用时间:XXXX

Git 中 pull 操作和 rebase 操作的不同

由于在开发过程中,pull 操作和 rebase 操作都是用来合并分支的,所以我就常常分不清这两个操作具体有什么区别,所以才有了这篇博客来做个简单区分,具体细致差别还请移步到官方文档:Git - Reference (git-scm.com) 1)pull 操作明确来说,实际是分为了两步操作:fetch + merge fetch:进行 pull 操作的时候,git 首先会将远程仓库中的所有远

VS Code SSH 远程连接服务器及坑点解决

背景 Linux服务器重装了一下,IP没有变化,结果VS Code再重连的时候就各种问题,导致把整个流程全部走了一遍,留个经验帖以备查看 SSH 首先确保Windows安装了ssh,通过cmd下ssh命令查看是否安装了。 没安装,跳转安装Windows下的ssh 对应的,也需要Linux安装ssh,本文是Ubuntu系统,使用以下命令安装: sudo apt updatesudo

Google Code Jam 2014(附官方题解)

2014年Google编程挑战赛 Problem A. Magic Trick Confused? Read the quick-start guide. Small input 6 points You have solved this input set. Note: To advance to the next rounds, you will need to s

HashSet的存储方式是把HashMap中的Key作为Set的对应存储项。

在 Java2中,有一套设计优良的接口和类组成了Java集合框架Collection,使程序员操作成批的数据或对象元素极为方便。这些接口和类有很多对抽象数据类型操作的API,而这是我们常用的且在数据结构中熟知的。例如Map,Set,List等。并且Java用面向对象的设计对这些数据结构和算法进行了封装,这就极大的减化了程序员编程时的负担。程序员也可以以这个集合框架为基础,定义更高级别的数据