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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

【CodeForces - 722D】Generating Sets(二分,贪心)

發(fā)布時(shí)間:2023/12/10 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【CodeForces - 722D】Generating Sets(二分,贪心) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題干:

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è)原數(shù)列a,你可以對(duì)任何一個(gè)數(shù)字進(jìn)行操作,令x變?yōu)?*x 或2*x+1。現(xiàn)在給你一個(gè)目標(biāo)數(shù)列b,讓你求一個(gè)原數(shù)列,原數(shù)列可能有多個(gè),要求你求的這個(gè)原數(shù)列最大的一個(gè)元素最小。注意原數(shù)列和目標(biāo)數(shù)列都必須滿足每個(gè)元素都不相同,但是在變化的過(guò)程中(也就是2*x,2*x+1這種變換過(guò)程中)不需要滿足這個(gè)條件。

    輸入:第一行一個(gè)數(shù)n,第二行n個(gè)數(shù)代表b數(shù)組。

    解題報(bào)告:

    將數(shù)字之間的變換畫出來(lái)發(fā)現(xiàn)是一棵二叉樹,所以每個(gè)bi變小的過(guò)程,是唯一的,因?yàn)楦腹?jié)點(diǎn)是唯一的。發(fā)現(xiàn)從最小的數(shù)開始貪心或從大的數(shù)開始貪心都不是很合適,所以二分上界,然后從大到小貪心,讓大的數(shù)字找地方放下,能放下則放下,給小的數(shù)字騰出盡可能大的空間,不難證明這種貪心的正確性。代碼是勉勉強(qiáng)強(qiáng)的O(n*lognlogn)的復(fù)雜度。

    當(dāng)然這題可以不用二分,直接拿個(gè)優(yōu)先隊(duì)列也可,這樣復(fù)雜度可以做到O(nlogn)。

    AC代碼:

    #include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<stack> #include<map> #include<vector> #include<unordered_set> #include<string> #include<cmath> #include<cstring> #define FF first #define SS second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 50000 + 6; int n,a[MAX],mx; vector<int> ans,tmp; unordered_set<int> ss; bool ok(int up) {tmp.clear();ss.clear();//從大到小進(jìn)行安排位置for(int x,i = 1; i<=n; i++) {x = a[i];while(x >= 1) {if(x<=up && ss.find(x) == ss.end()) {ss.insert(x);tmp.pb(x);break;}if(x&1) x=(x-1)/2;else x=x/2;}}return (int)tmp.size() == n; } int main() { // cout << log2(50000)*log2(50000)*log2(50000)*50000<<endl;cin >> n;for(int i = 1; i<=n; i++) scanf("%d",a+i),mx=max(mx,a[i]);sort(a+1,a+n+1); reverse(a+1,a+n+1);int l = 1,r = mx,mid;while(l<=r) {mid=(l+r)>>1;if(ok(mid)) r = mid-1,ans=tmp;else l = mid+1;}for(auto x : ans) printf("%d ",x);return 0 ; }

    ?

    總結(jié)

    以上是生活随笔為你收集整理的【CodeForces - 722D】Generating Sets(二分,贪心)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

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