Grids Everywhere
Practice
5 (1 votes)
Algorithms
Approved
Dinic's algorithm
Graphs
Hard
Open
Problem
75% Success 337 Attempts 50 Points 3s Time Limit 256MB Memory 1024 KB Max Code

A n x m grid consists of 'n' rows and 'm' columns. You are given 'n' elements, where each ni denotes the sum of elements of the ith row of the grid. You are also given 'm' elements, where each mi denotes the sum of elements of the ith column of the grid. You need to construct the grid by filling the desired cells with values in the range [1, 5] inclusive such that when elements arranged row-wise (a11, a12, a21, a22) represent the lexicographically shortest solution of the grid. Here aij represents the element at ith row and jth column.

Explanation:

Let there be two possible solutions for these row and column sums:

|a1 a2|

|a3 a4|

and

|b1 b2|

|b3 b4|

(a1, a2, a3, a4) is lexicographically smaller than (b1, b2, b3, b4) if first index 'i' where ai and bi differs, ai < bi satisfies.

Input:

There are 't' test cases. For each test case you will be given 'n' - number of rows and 'm' - number of columns. This is followed by a line containing 'n' space separated integers 'ni', sum of elements of the ith row. Next line contains 'm' space separated integers 'mi', sum of elements of the ith column.

Output:

You need to print the lexicographically shortest grid solution. There always exists a solution. This needs to be printed in 'n' lines, each line containing 'm' space separated elements of the 'ni' row.

As for a 2 x 2 grid print :

a11 a12

a21 a22

Constraints:

t<=50

1<=m,n<=40

Each element of grid will be [1,5]

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
4 votes
Tags:
AlgorithmsApprovedGraphsHardMatchingOpen