zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    SQL Server事务执行一半出错是否自动回滚整个事务 【转】
    html5 canvas做的俄罗斯方块
    laravel-admin 模型创建、数据迁移、以及关联模型控制器
    laravel-admin 安装
    Composer 安装时要求输入授权用户名密码?
    查找mysql中未提交的事务
    SSH 登录时出现如下错误:No supported key exchange algorithms
    MySQL运行一段时间后自动停止问题的排查
    浅谈PHP中的数组和JS中的数组
    MySQL中使用group_concat()函数数据被截取(有默认长度限制),谨慎!
  • 原文地址:https://www.cnblogs.com/hlmark/p/4297387.html
Copyright © 2011-2022 走看看