Preliminaries for Benelux Algorithm Programming Contest 2019 K题

Knapsack Packing

Problem statement
One of the most difficult things about going on a holiday is making sure your luggage does not exceed the maximum weight. You, chairman of the Backpacker’s Association for Packing Carry-ons, are faced with exactly this problem. You are going on a lovely holiday with one of your friends, but now most of your time is spent in frustration while trying to pack your backpack. In order to optimize this process, you and your friend have independently set upon trying to find better ways to pack.

After some time you have a serious breakthrough! Somehow you managed to solve the Knapsack problem in polynomial time, defying expectations everywhere. You are not interested in any theoretical applications, so you immediately return to your friend’s apartment in order to now quickly pack your backpack optimally.

When you arrive there, you find that your friend has set upon her own solution, namely to enumerate all possible packings. This means that all items you possibly wanted to bring are scattered across the entire apartment, and it would take a really long time to get all the items back together.
Luckily you can use the work your friend has done. For every possible subset of items that you can possibly bring, she has written down the total weight of these items. Alas, she did not write down what items were part of this total, so you do not know what items contributed to each total weight. If the original weights of the items formed a collection ( a 1 , … , a n ) (a_1, \dots, a_n) (a1,,an) of
non-negative integers, then your friend has written down the multiset.

{ ∑ i ∈ I a i ∣ I ⊂ { 1 , 2 , 3 , … , n } } = S \left\{ \sum\limits_{i \in I} a_{i} | I \subset\{1,2,3,\dots,n\}\right\} = S { iIaiI{ 1,2,3,,n}}=S

For example, if your friend had two items, and the weights of those two items are 2, 3, then your friend has written down

  • 0, corresponding to the empty set {};
  • 2, corresponding to the subset {2};
  • 3, corresponding to the subset {3};
  • 5, corresponding to the subset {2, 3}.

You want to reconstruct the weights of all the individual items so you can start using your Knapsack algorithm. It might have happened that your friend made a mistake in adding all these weights, so it might happen that her list is not consistent.

Input

  • One line containing a single integer 1 ≤ n ≤ 18 1\leq n\leq18 1n18 the number of items.
  • 2 n 2^n 2n lines each containing a single integer 0 ≤ w ≤ 228 0\leq w\leq228 0w228, the combined weight of a subset of the items. Every subset occurs exactly once.

Output
Output non-negative integers a 1 , … , a n a_1,\dots, a_n a1,,an on n n n lines in non-decreasing order such that S. ( a 1 , … , a n ) = { b 1 , … , b 2 n } (a_1,\dots, a_n) = \{b_1,\dots, b_{2n} \} (a1,,an)={ b1,,b2n}, provided that such integers exist. Otherwise, output a single line containing
impossible.

Sample input 1
1
0
5

Sample output 1
5

Sample input 2
3
7
5
2
4
1
6
3
0

Sample output 2
1
2
4

Sample input 3
2
0
1
2
4

Sample output 3
impossible

Sample input 4
2
0
1
1
2

Sample output 4
1
1

AC代码

#include 

using namespace std;

typedef long long ll;

multiset<int> s;

int main()
{
     
	ll n; 
	cin >> n;
	ll p = 1ll << n;
	while(p--)
	{
      
		int x; 
		cin >> x; 
		s.insert(x); 
	}
	vector<int> ans;
	while(n--)
	{
     
		auto v = *next(s.begin());
		ans.push_back(v);
		for(auto j = begin(s); j != end(s); ++j)
		{
     
			auto x = *j;
			auto it = prev(s.upper_bound(x+v));
			if(it == j || *it != x+v)
			{
     
				cout << "impossible" << endl;
				return 0;
			}
			s.erase(it);
		}
	}
	for(auto x : ans)
		cout << x << endl;
}

你可能感兴趣的:(Preliminaries for Benelux Algorithm Programming Contest 2019 K题)