• CodeChef Mahesh and his lost array


    Mahesh and his lost array

     
    Problem code: ANUMLA
     

    All submissions for this problem are available.

    Read problems statements in Mandarin Chinese and Russian as well.

    Mahesh got a beautiful array named A as a birthday gift from his beautiful girlfriend Namratha. There are N positive integers in that array. Mahesh loved the array so much that he started to spend a lot of time on it everyday. One day, he wrote down all possible subsets of the array. Then for each subset, he calculated the sum of elements in that subset and wrote it down on a paper. Unfortunately, Mahesh lost the beautiful array :(. He still has the paper on which he wrote all subset sums. Your task is to rebuild beautiful array A and help the couple stay happy :)

    Input

    The first line of the input contains an integer T denoting the number of test cases.
    First line of each test case contains one integer N, the number of elements in A.
    Second line of each test case contains 2^N integers, the values written on paper

    Output

    For each test case, output one line with N space separated integers in non-decreasing order.

    Constraints

    • 1T50
    • 1N15
    • 0Values on paper10^9
    • All input values are valid. A solution always exists

    Example

    Input
    2
    1
    0 10
    2
    0 1 1 2
    
    Output
    10
    1 1
    

    Explanation

    Test case #2
    For the array [1,1], possible subsets are {}, {1}, {1}, {1,1}, respective sums are 0, 1, 1, 2.

    给出序列的所有子集和,还原序列的元素

    从小到大枚举删数,然后遇到一个最小的即是序列中的数

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    const int N = 100010;
    int n , m  , tot , x[N] ;
    bool vis[N] , tag ;
    vector<int>ans;
    
    void Output() {
        for( int i = 0 ; i < ans.size(); ++i ){
            cout << ans[i] << ( i == n-1?'
    ':' ');
        }
    }
    
    int find( int num ) {
        int l = 0 , r = tot-1 , pos = -1 ;
        if( num > x[r] ) return -1 ;
        while( l <= r ) {
            int mid = (l+r)>>1;
            if( x[mid] == num ) {
                if( vis[mid] ) l = mid + 1 ;
                else pos = mid , r = mid - 1 ;
            }
            else if( x[mid] < num )
                l = mid + 1 ;
            else
                r = mid - 1 ;
        }
        return pos ;
    }
    
    void Run() {
        cin >> n ;
        tot = (1<<n); ans.clear();
        memset( vis , false , sizeof vis );
        for( int i = 0 ; i < tot ; ++i ) cin >> x[i] ;
        sort( x , x + tot );
        for( int i = 1 ; i < tot ; ++i ) if( !vis[i] ) {
            vis[i] = true; ans.push_back( x[i] );
            if( ans.size() == n ) break ;
            for( int j = 1 ; j < tot ; ++j ) if( vis[j] ) {
                int pos = find( x[i] + x[j] );
                if( pos == -1 || i == j ) continue ;
                vis[pos] = true ;
            }
        }
        Output();
    }
    int main()
    {
    //    freopen("in.txt","r",stdin);
        int _ ,cas =1 ; cin >> _ ;
        while(_--) Run() ;
    }
    View Code
  • 相关阅读:
    封装的图片预加载,数据加载到浏览器底部加载数据
    自己封装的弹出层插件
    在规定的时间内出现动画.html
    WEB前端资源集
    前端优化几项
    移动H5前端性能优化指南
    微信小程序IDE(微信web开发者工具)安装、破解手册--转载
    微信小程序开发—快速掌握组件及API的方法---转载
    STM32数据类型定义
    HDOJ 4802 GPA
  • 原文地址:https://www.cnblogs.com/hlmark/p/4297387.html
Copyright © 2020-2023  润新知