Mock CCC14S3 - Spreadsheet Sorting

View as PDF

Submit solution

Points: 7
Time limit: 1.0s
Memory limit: 16M

Author:
Problem type
2014 Mock CCC by Alex and Timothy

Over the years, Alice has collected the physical data of her friend and secret love interest, Bob. Now she's trying to find trends in the data so she can learn more about Bob, and thus get closer to him. Alice entered the data into her spreadsheet software, but learned that unfortunately, the software doesn't work as it should. Namely, the "sort by column" function is broken. Each time Alice clicks a column, she wants all the rows in the spreadsheet to be sorted in nondecreasing order by values from that column. If rows have the same values in the column she clicks, the relative order of these rows should be preserved after the sort. Given the original spreadsheet and the sequence of the columns that Alice clicked, you are to determine the sorted spreadsheet.

Input Specification

Line 1 of input contains two integers: R, the number of rows in the spreadsheet, and C, the number of columns in the spreadsheet (1 \le R, C \le 100).
The next R lines will each contain C integers between 1 and 10\,000 inclusive, representing single cells of the spreadsheet.
The next line of input will have an integer N (1 \le N \le 100), the number of sort commands that Alice makes.
The last N lines will each contain a number c_i (1 \le c_i \le C), indicating that she sorts by column c_i for the i-th command (1 \le i \le N).

Output Specification

The output should contain R lines with C integers per line, the sorted spreadsheet after the N clicks.

Sample Input

4 3
6 2 1
9 1 3
9 2 1
6 1 1
2
2
1

Sample Output

6 1 1
6 2 1
9 1 3
9 2 1

Explanation

Alice first sorts by column 2, then she sorts by column 1. The spreadsheet is sorted as follows:

Before sorting:    Sort by col 2:     Sort by col 1:
    6 2 1              9 1 3              6 1 1
    9 1 3     ---->    6 1 1     ---->    6 2 1
    9 2 1              6 2 1              9 1 3
    6 1 1              9 2 1              9 2 1

Comments


  • -1
    NakajimaWataru  commented on Feb. 17, 2021, 12:57 a.m.

    I keeps trying but still have 3 wrong answers in a row, I can't figure out what is wrong of my code. code: n = input().split(' ') q = [] for i in range(int(n[0])): q.append(input().split(' ')) t = input() for i in range(int(t)): idx = int(input()) q.sort(key=lambda x: x[idx-1]) for line in q: st = '' for row in line: st += row + " " print(st.rstrip())