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
  • 相关阅读:
    电脑知识
    编译器错误信息: CS0433: 类型“ASP.global_asax”同时存在于“c:/WINDOWS/Microsoft.NET/Framework/v2.0.50727...的解决方法
    windows平台下的oracle ORA-01031的解决方法
    .NET下使用HTTP请求的正确姿势
    EasyUI Datagrid 分页
    Js 运算符(加减乘除)
    Navicat 运行 Oracle 存储过程示例
    oracle数据库忘记sys(或system)账户密码
    SQL Server 死锁问题
    C# 给某个方法设定执行超时时间
  • 原文地址:https://www.cnblogs.com/hlmark/p/4297387.html
Copyright © 2011-2022 走看看