CF722D. Generating Sets[贪心 STL]
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:
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.
InputThe 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.
OutputPrint?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 51 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
題意:找一個集合X,使得集合y中的元素都可以由集合x進行*2或*2+1得到,可以操作由操作得來的元素,求最大元素最小的集合x
還有30分鐘開始做這道題
想到貪心方法了每次取最大x然后x/2,如果當前沒有x/2就加入,否則讓x/2再/2
然而當時讀錯題意了把新得到的加入了一個新集合里
WARN:是正整數,沒有0 // // main.cpp // d // // Created by Candy on 10/1/16. // Copyright ? 2016 Candy. All rights reserved. // #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <set> using namespace std; const int N=5e4+5; inline int read(){char c=getchar();int x=0,f=1;while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}return x; } int n,y[N],cnt=0; set<int> s; set<int>::iterator it; int main(int argc, const char * argv[]) {n=read();for(int i=1;i<=n;i++) {y[i]=read();s.insert(y[i]);}while(true){it=s.end(); it--;int x=*it;while(s.find(x)!=s.end()&&x) x>>=1;if(x==0) break;else{s.erase(it);s.insert(x);}}for(it=s.begin();it!=s.end();it++) printf("%d ",*it);return 0; }
?
?---恢復內容結束---
轉載于:https://www.cnblogs.com/candy99/p/5927450.html
總結
以上是生活随笔為你收集整理的CF722D. Generating Sets[贪心 STL]的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java中 将字符串时间 '2015-
- 下一篇: poj 3680 Intervals