Generating Sets 贪心

Posted LuZhiyuan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Generating Sets 贪心相关的知识,希望对你有一定的参考价值。

H - Generating Sets
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit Status

Description

You are given a set Y of ndistinct positive integers y1,?y2,?...,?yn.

Set X of ndistinct 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:

  1. Take any integer xi and multiply it by two, i.e. replace xi with xi.
  2. Take any integer xi, multiply it by two and add one, i.e. replace xi with 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.

Sample Input

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 

 

 题意:
n个数的数组y[1~n],每个数各不相同,求一个每个数各不相同的x数组x[1~n],使得x中的数经过若干次两种操作变成y数组。操作:x[i]=2*x[i],x[i]=2*x[i]+1;
求最大值最小的一个x数组。
代码:
//优先队列+map,将y数组存入优先队,用map标记每个y[i]是否在优先队列中,
//每次取最大的一个y[i],看队列中有没有y[i]/2,没有就加入y[i]/2,除去y[i],
//如果有再看y[i]/2/2有没有.....直到除到1,队列中还有1就说明不能再减小了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<map>
using namespace std;
int a[50004];
map<int,int>mp;
struct cmp{
    bool operator () (int &a,int &b){
        return a<b;
    }
};
int main()
{
    int n,x;
    scanf("%d",&n);
    priority_queue<int,vector<int>,cmp>q;
    for(int i=0;i<n;i++){
        scanf("%d",&x);
        mp[x]=1;
        q.push(x);
    }
    mp[0]=1;
    while(1){
        int x=q.top();
        while(x>0){
            if(mp[x/2]) x/=2;
            else{
                mp[x/2]=1;
                q.push(x/2);q.pop();
                break;
            }
        }
        if(x==0) break;
    }
    printf("%d",q.top());q.pop();
    while(!q.empty()){
        printf(" %d",q.top());
        q.pop();
    }
    printf("\n");
    return 0;
}

 

以上是关于Generating Sets 贪心的主要内容,如果未能解决你的问题,请参考以下文章

CodeForces 722D Generating Sets

CodeForces 722D Generating Sets

[codeforces722D]Generating Sets

UVA11925-Generating Permutations(贪心)

CodeForces 1042 F Leaf Sets 贪心

CF 1042F Leaf Sets