Codechef Mahesh and his lost array

Source: Internet
Author: User

Mahesh and his lost array problem code: Anumla
  • Submit
  • All submissions
All submissions to this problem is 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 is N positive integers in the that array. Mahesh loved the array so much, 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 a paper. Unfortunately, Mahesh lost the beautiful array:(. He still have 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 the test cases.
First line of all test case contains one integer N, the number of elements in A.
Second line of all test case contains 2^n integers, the values written on paper

Output

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

Constraints
    • 1 ≤ T ≤
    • 1 ≤ N ≤
    • 0 ≤ Values on paper ≤ 10^9
    • All input values is valid. A Solution always exists
Example
Input1020 1 1 2Output101 1
Explanation

Test Case #2
For the array [all], possible subsets is {}, {1}, {1}, {}, respective sums is 0, 1, 1, 2.

Give all the subsets of the sequence and the elements of the restore sequence

From small to large enumerate the deletions, and then encounter a minimum that is the number in the sequence

#include <bits/stdc++.h>using namespaceStd;typedefLong LongLL;Const intN =100010;intN, M, tot, x[n];BOOLVis[n], tag; vector<int>ans;voidOutput () { for(inti =0; I < ans.size (); ++i) {cout<< Ans[i] << (i = = n1?'\ n':' '); }}intFindintnum) {    intL =0, R = tot-1, pos =-1 ; if(num > X[r])return-1 ;  while(L <=r) {intMid = (l+r) >>1; if(X[mid] = =num) {            if(Vis[mid]) L = mid +1 ; Elsepos = Mid, R = mid-1 ; }        Else if(X[mid] <num) L= Mid +1 ; ElseR= Mid-1 ; }    returnPos;}voidRun () {cin>>N; Tot= (1<<N);    Ans.clear (); memset (Vis,false,sizeofvis);  for(inti =0; i < tot; ++i) Cin >>X[i]; Sort (x, x+tot);  for(inti =1; i < tot; ++i)if( !Vis[i]) {Vis[i]=true; Ans.push_back (X[i]); if(ans.size () = = N) Break ;  for(intj =1; J < tot; ++J)if(Vis[j]) {intpos = Find (X[i] +X[j]); if(pos = =-1|| i = = j)Continue ; Vis[pos]=true ; }} Output ();}intMain () {//freopen ("In.txt", "R", stdin);    int_, cas =1; CIN >> _ ;  while(_--) Run ();}
View Code

Codechef Mahesh and his lost array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.