日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

CF722D. Generating Sets[贪心 STL]

發(fā)布時(shí)間:2025/3/15 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CF722D. Generating Sets[贪心 STL] 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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
    題意:找一個(gè)集合X,使得集合y中的元素都可以由集合x(chóng)進(jìn)行*2或*2+1得到,可以操作由操作得來(lái)的元素,求最大元素最小的集合x(chóng)
    還有30分鐘開(kāi)始做這道題
    想到貪心方法了每次取最大x然后x/2,如果當(dāng)前沒(méi)有x/2就加入,否則讓x/2再/2
    然而當(dāng)時(shí)讀錯(cuò)題意了把新得到的加入了一個(gè)新集合里

    WARN:是正整數(shù),沒(méi)有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; }

    ?

    ?

    ---恢復(fù)內(nèi)容結(jié)束---

    轉(zhuǎn)載于:https://www.cnblogs.com/candy99/p/5927450.html

    總結(jié)

    以上是生活随笔為你收集整理的CF722D. Generating Sets[贪心 STL]的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。