Adjust Weights
Issue Description
In this Equalize Weights problem. There are N bags. Each bag contains a various variety of items. Goods can be of different weights. The goal is to match the weights in the bags based on constraints discussed listed below. Also, it is ensured that the bags can always be matched with the same overall weight.
- Initial number of goods guaranteed must be same prior to and after equalization
- Any number of swaps needed to do equalization, are permitted
When the weights are adjusted one needs to abide by output spec to publish out the result.
The outcome spec is as follows
- The components of the Bag which contains the lightest weight needs to be published in the very first line
- The following lightest Bag components must be published on the second line
- So on and so forth, until all Bags in addition to their contents are printed
- Contents within the Bag must be printed in non-decreasing order
Refer instance section for better understanding of restriction and print order
Input
First line includes an integer N signifying variety of bags
Following 2 N lines, each include the complying with
Initial line contains single integer G denoting the variety of items in a bag
Second line includes G space apart integer which represent weight of goods in that bag
Output
Determine which bag requires to take place first line and publish its materials on very first line by sticking to output requirements stated above
Similarly, identify which bag needs to get print on following N- 1 lines and print their components according to output requirements discussed above
Refer Example section for a far better understanding of exactly how to print the outcome
Restraints
0
0
0
Time Limit
1
Examples
Instance 1
Input
3
2
6 13
3
20 7 10
1
4
Output
4 6 10
7 13
20
Explanation
There are 3 bags.
Very first bag includes 2 items of weights 6 kg, 13 kg.
Second bag include 3 products of weights 20 kg, 7 kg, 10 kg.
Third bag consists of 1 products of weight 4 kg.
Bag 1 = [6, 13] => > overall weight = 19 kg
Bag 2 = [20, 7, 10] => > overall weight = 37 kg
Bag 3 = [4] => > overall weight = 4 kg
Action 1: swap goods of weight 6 kg from bag 1 and goods of weight 7 kg from bag 2
Bag 1 = [7, 13] => > complete weight = 20 kg
Bag 2 = [20, 6, 10] => > total weight = 36 kg
Bag 3 = [4] => > overall weight = 4 kg
Action 2: swap goods of 20 kg from bag 1 and goods of 4 kg from bag 3
Bag 1 = [7, 13] => > overall weight = 20 kg
Bag 2 = [4, 6, 10] => > complete weight = 20 kg
Bag 3 = [20] => > overall weight = 20 kg
Weights of bags are currently adjusted to 20 kg.
Currently we require to print the materials of each bag. For that, first type the goods in the non-decreasing order as below:
Bag 1 = [7, 13]
Bag 2 = [4, 6, 10]
Bag 3 = [20]
Least weighted good is 4 kg and its in Bag 2 So, print Bag 2 materials in initial line.
Next off the very least weighted products in the continuing to be bags is 7 kg and its in Bag 1 So, print contents of Bag 1 in the 2nd line.
Finally, publish the components of Bag 3 on line 3
Instance 2
Input
3
2
160 340
3
40 448 12
2
300 200
Result
12 40 448
160 340
200 300
Explanation
There are 3 bags.
Initial bag has 2 products of weights 160 kg, 340 kg.
Second bag have 3 items of weights 40 kg, 448 kg, 12 kg
3rd bag contain 2 items of weight 300 kg and 200 kg.
By the exact same treatment we will certainly obtain the final weights in bags as adhere to:
Bag 1 = [12, 40, 448]
Bag 2 = [160, 340]
Bag 3 = [200, 300]
[sociallocker id=”766″] [/sociallocker]