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 ...
随机推荐
- Spring 学习02
一.上节内容回顾 1 spring的概念 (1)核心:ioc和aop (2)spring一站式框架 2 spring的bean管理(xml) (1)bean实例化 (2)注入属性 (3)注入对象属性 ...
- JQuery 对表格的详细操作
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding=& ...
- 第8月第16天 django pil
1. https://github.com/chaonet/forum/ sudo easy_install --find-links http://www.pythonware.com/produ ...
- [转]避免头文件重复包含以及#ifndef 与 #program once 的区别
为了避免同一个文件被include多次,C/C++中有两种方式,一种是#ifndef方式,一种是#pragma once方式.在能够支持这两种方式的编译器上,二者并没有太大的区别,但是两者仍然还是有一 ...
- Java多线程学习(八)线程池与Executor 框架
目录 历史优质文章推荐: 目录: 一 使用线程池的好处 二 Executor 框架 2.1 简介 2.2 Executor 框架结构(主要由三大部分组成) 2.3 Executor 框架的使用示意图 ...
- 005_ss-link.info的ping探测工具
用小工具ping.py测试距离您最快的节点 #!/usr/bin/env python # coding: utf-8 """ A pure python ping im ...
- [How to]HBase集群备份方法
1.简介 当HBase数据库中存在非常重要的业务数据的时候为了保护数据的可以对数据进行备份处理.对于HBase来说从备份操作来看可分为离线备份和在线备份. 2. 前准备 在测试环境上准备有哦两套HBa ...
- 解决chrome运行报错unknown error: cannot get automation extension
今天把默认浏览器改成chrome,结果一运行脚本就报错,具体错误信息如下. FAILED CONFIGURATION: @BeforeClass beforeClassorg.openqa.selen ...
- Python的CGI编程实现-通过接口运行服务器py脚本
yum 安装apcche [apache]yum 安装Apache(Centos 6.9) https://www.cnblogs.com/lauren1003/p/5993654.html只需一行命 ...
- 抓包获取百度音乐API
这次抓包是获取手机APP中的数据包,共分为三个部分: 1.win7建立wifi 2.PC架设代理服务器 手机设置代理 3.抓包分析 一.win7建立wifi 在win7下搭建wifi非常简单,网上的教 ...