CodeChef Mahesh and his lost array
Mahesh and his lost arrayProblem 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
- 1 ≤ T ≤ 50
- 1 ≤ N ≤ 15
- 0 ≤ Values on paper ≤ 10^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 = ;
int n , m , tot , x[N] ;
bool vis[N] , tag ;
vector<int>ans; void Output() {
for( int i = ; i < ans.size(); ++i ){
cout << ans[i] << ( i == n-?'\n':' ');
}
} int find( int num ) {
int l = , r = tot- , pos = - ;
if( num > x[r] ) return - ;
while( l <= r ) {
int mid = (l+r)>>;
if( x[mid] == num ) {
if( vis[mid] ) l = mid + ;
else pos = mid , r = mid - ;
}
else if( x[mid] < num )
l = mid + ;
else
r = mid - ;
}
return pos ;
} void Run() {
cin >> n ;
tot = (<<n); ans.clear();
memset( vis , false , sizeof vis );
for( int i = ; i < tot ; ++i ) cin >> x[i] ;
sort( x , x + tot );
for( int i = ; i < tot ; ++i ) if( !vis[i] ) {
vis[i] = true; ans.push_back( x[i] );
if( ans.size() == n ) break ;
for( int j = ; j < tot ; ++j ) if( vis[j] ) {
int pos = find( x[i] + x[j] );
if( pos == - || i == j ) continue ;
vis[pos] = true ;
}
}
Output();
}
int main()
{
// freopen("in.txt","r",stdin);
int _ ,cas = ; cin >> _ ;
while(_--) Run() ;
}
CodeChef Mahesh and his lost array的更多相关文章
- scau 2015寒假训练
并不是很正规的.每个人自愿参与自愿退出,马哥找题(马哥超nice么么哒). 放假第一周与放假结束前一周 2015-01-26 http://acm.hust.edu.cn/vjudge/contest ...
- codechef AUG17 T1 Chef and Rainbow Array
Chef and Rainbow Array Problem Code: RAINBOWA Chef likes all arrays equally. But he likes some array ...
- 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 ...
- 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. T ...
- CodeChef - ELHIDARR Find an element in hidden array(互动题)题解
题意:有一串不递减的串,串中的任意元素都有k个,除了一个元素,他只有1 <= n < k-1个,你现在能向oj做出以下操作: 输出:1 pos,oj会返回pos位置的元素值 输出:2 va ...
- CF&&CC百套计划2 CodeChef December Challenge 2017 Chef And Easy Xor Queries
https://www.codechef.com/DEC17/problems/CHEFEXQ 题意: 位置i的数改为k 询问区间[1,i]内有多少个前缀的异或和为k 分块 sum[i][j] 表示第 ...
- CF&&CC百套计划2 CodeChef December Challenge 2017 Chef and Hamming Distance of arrays
https://www.codechef.com/DEC17/problems/CHEFHAM #include<cstdio> #include<cstring> #incl ...
- CODECHEF Chef and Churus 解题报告
[CODECHEF]Chef and Churus Description 有一个长度为\(n\)的数组\(A\),有\(n\)个函数,第\(i\)个函数的值为\(\sum_{j=l_i}^{r_i} ...
- Codechef Course Selection
Home » Practice(Hard) » Course Selection Course Selection Problem Code: RINSubmit https://www.codech ...
随机推荐
- CentOS下性能监测工具 dstat
原文链接:http://www.bkjia.com/Linuxjc/935113.html 参考链接:https://linux.cn/article-3215-1.html,http://lhfli ...
- 记录卸载5.7版本MySQL并安装5.6版本MySQL
新版本有些问题很烦,也没时间去找解决办法,只好用回5.6,首先卸载6.7的MySQL: sudo apt-get autoremove --purge mysql-server-* apt remov ...
- 漫谈五种IO模型
阅读目录 1 基础知识回顾 2 I/O模式 3 事件驱动编程模型 网络编程里常听到阻塞IO.非阻塞IO.同步IO.异步IO等概念,搞清楚这些概念之前,还得先回顾一些基础的概念. 1 基础知识回顾 注意 ...
- 【串线篇】spring boot配置文件加载位置
springboot 启动会扫描以下位置的application.properties或者application.yml文件作为Spring boot的默认配置文件 (1)–file:./config/ ...
- 00.斐波那契数列第n项
# 斐波那契数列第n项 # 1 1 2 3 5 8 def fib(n): if n <= 2: return 1 else: return fib(n-2)+fib(n-1) def fib2 ...
- Fabric的使用总结
环境说明 建议python版本:2.7 fabric版本:最新版(当前为1.10.2) fabric安装 通过pip.py的方式安装(详细可以在fabric官网了解) fabric执行py文件的方法, ...
- 【leetcode】449. Serialize and Deserialize BST
题目如下: Serialization is the process of converting a data structure or object into a sequence of bits ...
- 第六周作业—N42-虚怀若谷
一.自建yum仓库,分别为网络源和本地源 [root@centos7 ~]# cd /etc/yum.repos.d/ [root@centos7 yum.repos.d]# mkdir bak #建 ...
- 多个webView嵌套在UIScrollView里面内存优化
之前一直纠结在使用UIWebView上,发现加载6个UIWebView占用了290M左右内存,切换时还会增加.后来看到一篇文章:http://blog.csdn.net/kongu/article/d ...
- 怎么测试php代码
没有任何一名程序员可以一气呵成.完美无缺的在不用调试的情况下完成一个功能或模块.调试实际分很多种情况. 暴力调试 这种方式简单粗暴,一般PHP程序员都会用,那就是浏览器调试,在编辑器内写完代码后随后打 ...