Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) -- D. Generating Sets(贪心)
2016-10-02 19:13
489 查看
大体题意:
给你长度为n 的数组y,求一个数组x,满足y中的每一个元素都可以由x中的元素 进行乘以2 或者 乘以2加1 的操作来得到!要求输出x中的最大元素尽可能小的结果?
思路:
比赛没有出,赛后补的!
贪心思路:
先把刚开始的数放到set里,不断取出最大值,然后给当前最大值找到第一个合适的数,不断的找,不断的处理,直到当前最大值不能在小了,结束循环!
这种贪心相当于每一步都找到最大值,每一次 都给最大值 优化一步!
#include <bits/stdc++.h>
using namespace std;
set<int>s;
set<int>::iterator it;
int main(){
int n;
scanf("%d",&n);
for (int i = 0; i < n; ++i){
int x;
scanf("%d",&x);
s.insert(x);
}
while(1){
int x = *s.rbegin();
s.erase(x);
int val = x/2;
bool ok = 0;
while(1){
if(val == 0)break;
if (val && !s.count(val)){
ok = 1;
s.insert(val);
break;
}
val/=2;
}
if (!ok){
s.insert(x);
break;
}
}
for (it = s.begin(); it != s.end(); ++it){
if (it != s.begin())printf(" ");
printf("%d",*it);
}
puts("");
return 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:
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.
Examples
input
output
input
output
input
output
给你长度为n 的数组y,求一个数组x,满足y中的每一个元素都可以由x中的元素 进行乘以2 或者 乘以2加1 的操作来得到!要求输出x中的最大元素尽可能小的结果?
思路:
比赛没有出,赛后补的!
贪心思路:
先把刚开始的数放到set里,不断取出最大值,然后给当前最大值找到第一个合适的数,不断的找,不断的处理,直到当前最大值不能在小了,结束循环!
这种贪心相当于每一步都找到最大值,每一次 都给最大值 优化一步!
#include <bits/stdc++.h>
using namespace std;
set<int>s;
set<int>::iterator it;
int main(){
int n;
scanf("%d",&n);
for (int i = 0; i < n; ++i){
int x;
scanf("%d",&x);
s.insert(x);
}
while(1){
int x = *s.rbegin();
s.erase(x);
int val = x/2;
bool ok = 0;
while(1){
if(val == 0)break;
if (val && !s.count(val)){
ok = 1;
s.insert(val);
break;
}
val/=2;
}
if (!ok){
s.insert(x);
break;
}
}
for (it = s.begin(); it != s.end(); ++it){
if (it != s.begin())printf(" ");
printf("%d",*it);
}
puts("");
return 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:
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.
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
相关文章推荐
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined)D. Generating Sets(贪心)
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D. Generating Sets 贪心
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D. Generating Sets 贪心
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D. Generating Sets 贪心+优先队列
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D. Generating Sets __ dfs+优先队列+贪心
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) -- A. Broken Clock (贪心)
- 【Intel Code Challenge Elimination Round (Div1 + Div2, combined) D】【贪心 暴力 SET】Generating Sets n个不同的x变
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined)C. Destroying Array(想法题/并查集)
- codeforces(Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) )(C,D)
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined)(set容器里count函数以及加强for循环)
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) A. Broken Clock 水题
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C 并查集
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array 双向链表+反向做+优先队列
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) B. Verse Pattern 水题
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array 带权并查集
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) [D] Destroying Array
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined)
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined)E. Research Rover (容斥Lucas)
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array -- 逆向思维
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array 带权并查集