Problem Link
#include <iostream>
#include <set>
#include <cstring>
#include <algorithm>
using namespace std;
#define ll long long
const int N = 32768;
int main()
{
int n, t;
long long arr[N + 1];
cin >> t;
while(t--)
{
cin >> n;
multiset<ll> s;
long long sum[N + 1];
long long element[N + 1];
int maxIndex = 1 << n;
for(int i = 0; i < maxIndex; i++)
{
cin >> arr[i];
}
sort(arr, arr + maxIndex);
if(n == 1)
{
cout << arr[1] << endl;
continue;
}
if(n == 2)
{
cout << arr[1] << " " << arr[2] << endl;
continue;
}
int index = 0;
int sumIndex = 0;
for(int i = 1; i < maxIndex; i++)
{
int expected = -1;
if(!s.empty())
{
expected = *s.begin();
}
if(arr[i] == expected)
{
s.erase(s.begin());
}
else
{
element[index++] = arr[i];
int lastIndex = sumIndex;
for(int j = 0; j < lastIndex; j++)
{
sum[sumIndex] = sum[j] + arr[i];
s.insert(sum[sumIndex]);
sumIndex++;
}
sum[sumIndex++] = arr[i];
}
}
for(int i = 0; i < index; i++)
{
cout << element[i] << " ";
}
cout << endl;
}
return 0;
}
#include <iostream>
#include <set>
#include <cstring>
#include <algorithm>
using namespace std;
#define ll long long
const int N = 32768;
int main()
{
int n, t;
long long arr[N + 1];
cin >> t;
while(t--)
{
cin >> n;
multiset<ll> s;
long long sum[N + 1];
long long element[N + 1];
int maxIndex = 1 << n;
for(int i = 0; i < maxIndex; i++)
{
cin >> arr[i];
}
sort(arr, arr + maxIndex);
if(n == 1)
{
cout << arr[1] << endl;
continue;
}
if(n == 2)
{
cout << arr[1] << " " << arr[2] << endl;
continue;
}
int index = 0;
int sumIndex = 0;
for(int i = 1; i < maxIndex; i++)
{
int expected = -1;
if(!s.empty())
{
expected = *s.begin();
}
if(arr[i] == expected)
{
s.erase(s.begin());
}
else
{
element[index++] = arr[i];
int lastIndex = sumIndex;
for(int j = 0; j < lastIndex; j++)
{
sum[sumIndex] = sum[j] + arr[i];
s.insert(sum[sumIndex]);
sumIndex++;
}
sum[sumIndex++] = arr[i];
}
}
for(int i = 0; i < index; i++)
{
cout << element[i] << " ";
}
cout << endl;
}
return 0;
}
Comments
Post a Comment