国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Codeforces 722D Generating Sets【優先隊列+貪心】

2019-11-06 06:14:54
字體:
來源:轉載
供稿:網友

D. Generating Setstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

You are given a set Y of n distinct positive integersy1,?y2,?...,?yn.

Set X of ndistinct positive integers x1,?x2,?...,?xn is said togenerate set Y if one can transformX to Y by applying some number of the following two Operation to integers inX:

Take any integer xi and multiply it by two, i.e. replacexi with2·xi.Take any integer xi, multiply it by two and add one, i.e. replacexi with2·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 setX that generates Y and themaximum 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 inY.

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 generateY and the maximum element of which is minimum possible. If there are several such sets, print any of them.

ExamplesInput
51 2 3 4 5Output
4 5 2 3 1 Input
615 14 3 13 1 12Output
12 13 14 7 3 1 Input
69 7 13 17 5 11Output
4 5 2 6 3 1 

題目大意:

讓你構造一個長度為N的數組X【】,使得其能夠通過兩種操作得到數組Y【】;

現在給你一個數組Y【】,讓你構造一個可行X【】,使得其中最大值盡可能的小。

兩種操作:

選擇x【i】,使得其變成X【i】*2或者是X【i】*2+1.....

保證Y【】是一個不包含重復元素的數組,要求X【】也不能包含重復元素。而且所有元素大于0.

思路:

1、考慮問題本質,其實我們最開始可以設定ans【】(X【】)就是Y【】;

然后盡可能的縮小最大值,來尋找一個最終序列,使得X【】中最大元素盡可能的小。

2、那么首先我們將數組Y【】從大到小排序,然后全部塞到一個優先隊列中,每次我們拿出最大值,肯定想要做到的就是將這個數變小,變成一個和此時隊列中所有元素不相同的小值。

那么我們每一次取隊頭元素,將其盡可能的變小,如果可以變小,再將變小的值塞進隊列,繼續取隊頭,循環往復。

直到不能變小為止,停止操作即可。

Ac代碼:

#include<stdio.h>#include<algorithm>#include<string.h>#include<map>#include<queue>using namespace std;struct node{    int val;    friend bool operator <(node a,node b)    {        return a.val<b.val;    }}nod;int a[50050];int ans[50050];int b[500];int main(){    int n;    while(~scanf("%d",&n))    {        for(int i=0;i<n;i++)scanf("%d",&a[i]);        sort(a,a+n);reverse(a,a+n);        priority_queue<node>s;        map<int ,int >ss;        for(int i=0;i<n;i++)        {            nod.val=a[i];            ss[a[i]]=1;            s.push(nod);        }        while(1)        {            int flag=0;            nod=s.top();            while(nod.val)            {                nod.val/=2;                if(ss[nod.val]==0&&nod.val!=0)                {                    flag=1;                    ss[nod.val]=1;                    s.pop();                    s.push(nod);                    break;                }            }            if(flag==0)break;        }        while(!s.empty())        {            nod=s.top();            s.pop();            printf("%d ",nod.val);        }        printf("/n");    }}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 隆德县| 五家渠市| 芜湖县| 天长市| 花垣县| 额尔古纳市| 黄骅市| 克拉玛依市| 舞钢市| 连山| 蚌埠市| 扬州市| 招远市| 什邡市| 福贡县| 潮安县| 抚州市| 安平县| 西安市| 邵阳市| 三台县| 侯马市| 班戈县| 长海县| 福州市| 邵东县| 嘉兴市| 弥勒县| 汾西县| 陵水| 安远县| 永顺县| 莱州市| 肇源县| 南开区| 广灵县| 荣成市| 澄城县| 通河县| 民县| 正镶白旗|