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 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.
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.
Sample Input
5
1 2 3 4 5
Sample Output
4 5 2 3 1
Hint
题意
一个数x,可以变成2x,或者变成2x+1,可以变化若干次
现在给你n个不同的数Y,你需要找到n个不同的x,使得这n个不同的x经过变化之后,能够得到Y数组,你要使得最初的最大值最小。
问你应该怎么做。
题解:
贪心,每次选择最大的数,然后使得最大数变小即可,能变就变,用一个set去维护就好了。
代码
#include<bits/stdc++.h>
using namespace std;
set<int> S;
int main()
{
int n;scanf("%d",&n);
for(int i=1;i<=n;i++)
{
int x;scanf("%d",&x);
S.insert(-x);
}
while(1)
{
int x=-*S.begin();
int k = *S.begin();
x/=2;
while(x)
{
if(S.find(-x)==S.end())
{
S.insert(-x);
break;
}
x/=2;
}
if(x==0)
{
for(auto it=S.begin();it!=S.end();it++)
cout<<-*it<<" ";
cout<<endl;
return 0;
}
S.erase(k);
}
return 0;
}
Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D. Generating Sets 贪心的更多相关文章
- 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 ...
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) B. Verse Pattern 水题
B. Verse Pattern 题目连接: http://codeforces.com/contest/722/problem/B Description You are given a text ...
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined)
A. Broken Clock time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined)(set容器里count函数以及加强for循环)
题目链接:http://codeforces.com/contest/722/problem/D 1 #include <bits/stdc++.h> #include <iostr ...
- 二分 Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D
http://codeforces.com/contest/722/problem/D 题目大意:给你一个没有重复元素的Y集合,再给你一个没有重复元素X集合,X集合有如下操作 ①挑选某个元素*2 ②某 ...
- 线段树 或者 并查集 Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C
http://codeforces.com/contest/722/problem/C 题目大意:给你一个串,每次删除串中的一个pos,问剩下的串中,连续的最大和是多少. 思路一:正方向考虑问题,那么 ...
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array 带权并查集
C. Destroying Array 题目连接: http://codeforces.com/contest/722/problem/C Description You are given an a ...
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) A. Broken Clock 水题
A. Broken Clock 题目连接: http://codeforces.com/contest/722/problem/A Description You are given a broken ...
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array
C. Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard ...
随机推荐
- disabled属性对form表单向后台传值的影响
在form表单里,如果对input加入disabled="disabled"或disabled="true"等属性,form表单提交的时候,就不会传值到后台. ...
- Bower使用笔记
全局安装bower $ npm install -g bower 检测成功 $ bower help 在项目根目录下进行安装(最新版本),会自动生成一个bower_components文件夹(如果在c ...
- pandas重置索引的几种方法探究
pandas重置索引的几种方法探究 reset_index() reindex() set_index() 函数名字看起来非常有趣吧! 不仅如此. 需要探究. http://nbviewer.jupy ...
- 这两天自己模仿写的一个Asp.Net的显示分页方法 附加实体转换和存储过程
之前自己一直用Aspnetpager控件来显示项目中的分页,但是每次都要拖一个aspnetpager的控件进去,感觉很不舒服,因为现在自己写的webform都不用服务器控件了,所以自己仿照aspnet ...
- JavaScript编写风格指南 (一)
//参考<编写可维护的Javascript> 一:缩进// 第一行的层级由4个空格组成,避免使用制表符tab进行缩进 //好的写法if (true) { doSomething() ...
- HDU 1308 What Day Is It?(模拟题)
解题报告:输入一个年月日,让你求出那一天是星期几,但是做这题之前必须先了解一点历史.首先在1582年之前,判断是否是闰年的标准是只要能被四整除就是闰年, 然后在1752年9月2号的后的11天被抹去了, ...
- Python多线程-2(线程共享全局变量)
例子: from threading import Thread,Lock from time import sleep, ctime global_num = [] def func1(): glo ...
- Vue零散知识点
1.vue中的<router-view></router-view>的作用,它是和vue的路由相结合的,它的作用是将路由匹配到的组件渲染在里面.比如说你要跳转,如何没有< ...
- Java abstract 关键字
abstract是声明抽象类和抽象方法的关键字 包含抽象方法的类叫抽象类,如果一个类中包含一个或多个抽象方法,该类必须被限定为抽象的,否则编译器会报错,抽象类不可创建对象,创建抽象类的对象编译器会报错 ...
- ZCTF-2017 比赛总结
这次ZCTF办的还是相当不错的,至少对于Pwn来说是能够让人学习到一些东西. 第一天做的不是很顺利,一直卡在一道题上不动.第二天队友很给力,自己的思路也开阔起来了. 关于赛题的优点 我觉得这次的Pwn ...