zoukankan      html  css  js  c++  java
  • CF722D. Generating Sets[贪心 STL]


    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:

    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.

    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 

    题意:找一个集合X,使得集合y中的元素都可以由集合x进行*2或*2+1得到,可以操作由操作得来的元素,求最大元素最小的集合x

    还有30分钟开始做这道题 
    想到贪心方法了每次取最大x然后x/2,如果当前没有x/2就加入,否则让x/2再/2
    然而当时读错题意了把新得到的加入了一个新集合里

    WARN:是正整数,没有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;
    }
    
    
    
     


    ---恢复内容结束---

  • 相关阅读:
    zoj 3279 线段树 OR 树状数组
    fzu 1962 树状数组 OR 线段树
    hdu 5057 块状链表
    hdu3487 Play with Chain
    bzoj 1588营业额统计(HNOI 2002)
    poj2823 Sliding Window
    poj2828 Buy Tickets
    poj2395 Out of Hay
    poj3667 Hotel
    poj1703 Lost Cows
  • 原文地址:https://www.cnblogs.com/candy99/p/5927450.html
Copyright © 2011-2022 走看看