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?‘\n‘:‘ ‘);
    }
}

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() ;
}

时间: 2024-09-30 20:51:16

CodeChef Mahesh and his lost array的相关文章

codechef AUG17 T1 Chef and Rainbow Array

Chef and Rainbow Array Problem Code: RAINBOWA Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays. An array is Rainbow if it has the following structure: First a1 elements equal 1.

codechef AUG17 T5 Chef And Fibonacci Array

Chef has an array A = (A1, A2, ..., AN), which has N integers in it initially. Chef found that for i ≥ 1, if Ai > 0, Ai+1 > 0, and Ai+2 exists, then he can decrease both Ai, andAi+1 by one and increase Ai+2 by one. If Ai+2 doesn't exist, but Ai >

CodeChef - ELHIDARR Find an element in hidden array(二分交互)

Find an element in hidden array There is an array of length N consisting of non-negative integers. The array is sorted in non-decreasing order. Each number in the array appears exactly K times, except one element, which appears at least once, but les

Codechef Course Selection

Home » Practice(Hard) » Course Selection Course Selection Problem Code: RINSubmit https://www.codechef.com/problems/RIN All submissions for this problem are available. Read problems statements in Mandarin Chineseand Russian. Rin is attending a univer

CF&amp;&amp;CC百套计划2 CodeChef December Challenge 2017 Chef and Hamming Distance of arrays

https://www.codechef.com/DEC17/problems/CHEFHAM #include<cstdio> #include<cstring> #include<iostream> using namespace std; #define N 100001 int a[N],b[N]; int sum[N],wh[N][2]; int num1[N],num2[N]; void read(int &x) { x=0; char c=getc

PHP Warning: array_multisort(): Array sizes are inconsistent

array_multisort() 函数返回排序数组.您可以输入一个或多个数组.函数先对第一个数组进行排序,接着是其他数组,如果两个或多个值相同,它将对下一个数组进行排序. 遇到这报错是两个数组对比不一致导致的, 如果是一维数组与二维数组进行排序可以用以下方法解决: 使用这个方法,会比较麻烦些,要将age提取出来存储到一维数组里,然后按照age升序排列.具体代码如下: 复制代码代码如下: $ages = array();foreach ($users as $user) {    $ages[]

详解go语言的array和slice 【二】

上一篇  详解go语言的array和slice [一]已经讲解过,array和slice的一些基本用法,使用array和slice时需要注意的地方,特别是slice需要注意的地方比较多.上一篇的最后讲解到创建新的slice时使用第三个索引来限制slice的容量,在操作新slice时,如果新slice的容量大于长度时,添加新元素依然后使源的相应元素改变.这一篇里我会讲解到如何避免这些问题,以及迭代.和做为方法参数方面的知识点. slice的长度和容量设置为同一个值 如果在创建新的slice时我们把

JavaScript的进阶之路(三)引用类型之Object类型和Array类型

引用类型 Object类型 function a(num){ if(num>3){ a(--num); } console.log(num); } a(5); //如何创建对象的实例 var obj1= new Object(); console.log(obj1); obj1.name="吴琼"; obj1.age=28; console.log(obj1.name+" "+obj1.age); //对象字面量语法 ,有点封装的感觉 var obj2 = {

LeetCode 442. Find All Duplicates in an Array (在数组中找到所有的重复项)

Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements that appear twice in this array. Could you do it without extra space and in O(n) runtime? Example: Input: [4,3,2,7,