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 2·xi.
  2. 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.

Sample Input

Input

51 2 3 4 5

Output

4 5 2 3 1 

Input

615 14 3 13 1 12

Output

12 13 14 7 3 1 

Input

69 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;
}
时间: 2024-12-15 07:01:56

Generating Sets 贪心的相关文章

CodeForces 1042 F Leaf Sets 贪心

Leaf Sets 题意:给你一棵树,树上有n个点,只有一条边的点叫做叶子,现在要求把所有的叶子分组,每个组内的所有叶子的距离都不能大于k. 题解: 我们可以随意找一个不是叶子的节点当做这颗树的根节点,这样这棵树中叶子就不会出现在上方了,现在我们先把所有的叶子都单独当做一个集合来. 假设现在我们在处理以u为根的这个子树信息, 我们可以得到u子树内的叶子都到u的这个地方的信息,对这些信息来说,我们把距离都sort一遍,然后看一下是不是能合并,能合并就把信息合并一下,然后在把u的信息记为 min (

CodeForces 722D Generating Sets

贪心,优先队列. 每次变最大的数,变到最大的能变的一个就停止.如果发现最大的数不能变小,那么输出. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<map> #

[codeforces722D]Generating Sets

试题描述 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

URAL 1727. Znaika&amp;#39;s Magic Numbers(数学 vector)

主题链接:http://acm.timus.ru/problem.aspx?space=1&num=1727 1727. Znaika's Magic Numbers Time limit: 0.5 second Memory limit: 64 MB Znaika has many interests. For example, now he is investigating the properties of number sets. Znaika writes down some set

挖点坑

想写的题都列在这里吧 bzoj1061: [Noi2008]志愿者招募 bzoj1018: [SHOI2008]堵塞的交通 cf715B.Complete The Graph bzoj1040: [ZJOI2008]骑士 cf722D. Generating Sets cf718C.Sasha and Array

URAL 1727. Znaika&#39;s Magic Numbers(数学 vector)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1727 1727. Znaika's Magic Numbers Time limit: 0.5 second Memory limit: 64 MB Znaika has many interests. For example, now he is investigating the properties of number sets. Znaika writes down some set

贪心 + 并查集 之 CODE[VS] 1069 关押罪犯 2010年NOIP全国联赛提高组

/* 贪心 + 并查集 之 CODE[VS] 1069 关押罪犯  2010年NOIP全国联赛提高组 两座监狱,M组罪犯冲突,目标:第一个冲突事件的影响力最小. 依据冲突大小,将M组罪犯按从大到小排序,按照排序结果,依次把每组罪犯分开放入两个监狱, 直到当前这组罪犯已经在同一个监狱中了,此时即为答案. 实现: 1)通过不在同一个监狱的罪犯,推断出在同一个监狱的罪犯.(依据:一共就两个监狱)      ftr[b] = a+n   // a和b是在不同监狱 ftr[c] = a+n   // a和

POJ 1456 Supermarket 区间问题并查集||贪心

F - Supermarket Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1456 Appoint description:  System Crawler  (2015-11-30) Description A supermarket has a set Prod of products on sale. It earns a p

(贪心 + 并查集优化) poj 1456

Supermarket Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9452   Accepted: 4067 Description A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold by a deadline dx that is measured as an int