Star Showdown
InterviewTime limit2sMemory limit128 MB
Given row and column game counts, build the 0/1 matrix with those degrees that is lexicographically smallest row by row, or print -1 if none exists.
- Level
Medium7 of 10
- Topics
- Greedy, Graph, Implementation, Math
- Solved
- No attempts yet
Problem
The best StarCraft team on the west side of the city is Jimin's team, and the best on the east side is Hansu's team. The two teams decide to face off to settle who is the best in the city.
Jimin's team has players and Hansu's team has players. Each player has a fixed number of games they must play, and this number may differ from player to player.
The match table must satisfy all of the following rules.
- Every game is played between one player from Jimin's team and one player from Hansu's team.
- The same pairing may occur at most once.
- Every player must play exactly the number of games assigned to them.
- If several valid tables exist, choose the one that is smallest in lexicographic order.
The table is an matrix. Rows correspond to Jimin's team and columns to Hansu's team. If cell is , then player of Jimin's team plays against player of Hansu's team; if it is , they do not play.
The lexicographic comparison of two tables is defined as follows. First find the first row in which the two matrices differ, then find the first column in that row where they differ. The table with in cell comes first.
Given the number of games each player on each team must play, write a program that prints the lexicographically smallest table.
Input
The first line contains , the number of players on Jimin's team, and , the number of players on Hansu's team. The second line lists the number of games each player on Jimin's team must play, and the third line lists the number of games each player on Hansu's team must play. and are natural numbers at most , and each game count is a natural number at most or .
Output
Print the table on lines. Each line is an -character string formed by concatenating that row's values with no separators. If no valid table exists, print .