CF1500C.Matrix Sorting
省选/NOI-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given two tables A and B of size n×m.
We define a sorting by column as the following: we choose a column and reorder the rows of the table by the value in this column, from the rows with the smallest value to the rows with the largest. In case there are two or more rows with equal value in this column, their relative order does not change (such sorting algorithms are called stable).
You can find this behavior of sorting by column in many office software for managing spreadsheets. Petya works in one, and he has a table A opened right now. He wants to perform zero of more sortings by column to transform this table to table B.
Determine if it is possible to do so, and if yes, find a sequence of columns to sort by. Note that you do not need to minimize the number of sortings.
给你两个大小为 n×m 的表格 A 和 B。
我们定义“按列排序”如下:选择某一列,并依据该列中的数值对表格的行进行重排,使得数值小的行排在前面、数值大的行排在后面。若该列中存在两行或多行数值相等,则这些行之间的相对顺序保持不变(此类排序算法称为稳定排序)。
许多电子表格办公软件中均具有这种按列排序的行为。Petya 正在使用其中一款软件,当前正打开表格 A。他希望执行零次或多次按列排序操作,将表格 A 变为表格 B。
请判断是否可行;若可行,请给出一个合法的排序列序列(即每次排序所选的列号)。注意:你无需最小化排序次数。
输入格式
The first line contains two integers n and m (1≤n,m≤1500) — the sizes of the tables.
Each of the next n lines contains m integers ai,j (1≤ai,j≤n), denoting the elements of the table A.
Each of the next n lines contains m integers bi,j (1≤bi,j≤n), denoting the elements of the table B.
第一行包含两个整数 n 和 m(1≤n,m≤1500)—— 表格的尺寸。
接下来的 n 行,每行包含 m 个整数 ai,j(1≤ai,j≤n),表示表格 A 的元素。
再接下来的 n 行,每行包含 m 个整数 bi,j(1≤bi,j≤n),表示表格 B 的元素。
输出格式
If it is not possible to transform A into B, print −1.
Otherwise, first print an integer k (0≤k≤5000) — the number of sortings in your solution.
Then print k integers c1,…,ck (1≤ci≤m) — the columns, by which Petya needs to perform a sorting.
We can show that if a solution exists, there is one in no more than 5000 sortings.
如果无法将 A 变换为 B,则输出 −1。
否则,首先输出一个整数 k(0≤k≤5000)—— 表示你的解中排序操作的次数。
然后输出 k 个整数 c1,…,ck(1≤ci≤m)—— 表示 Petya 需要依次按这些列进行排序。
可以证明:若解存在,则必存在一个至多包含 5000 次排序操作的解。
输入输出样例
输入#1
2 2 2 2 1 2 1 2 2 2
输出#1
1 1
输入#2
3 3 2 3 2 1 3 3 1 1 2 1 1 2 1 3 3 2 3 2
输出#2
2 1 2
输入#3
2 2 1 1 2 1 2 1 1 1
输出#3
-1
输入#4
4 1 2 2 2 1 1 2 2 2
输出#4
1 1
说明/提示
Consider the second example. After the sorting by the first column the table becomes
\\begin{matrix} 1&3&3\\\\ 1&1&2\\\\ 2&3&2. \\end{matrix}After the sorting by the second column the table becomes
\\begin{matrix} 1&1&2\\\\ 1&3&3\\\\ 2&3&2, \\end{matrix}and this is what we need.
In the third test any sorting does not change anything, because the columns are already sorted.
考虑第二个例子。按第一列排序后,表格变为
112313322.
再按第二列排序后,表格变为
112133232,
这正是我们所需要的。
在第三个测试用例中,任何排序操作都不会改变表格,因为各列本身已经有序。
输入解题思路,AI测评打分。不知道怎么写?