Problem E
Acceptable Seating Arrangements
Charlie is managing a classroom. The seats in the classroom are arranged in a grid with rows and columns. Each student has a distinct height.
A configuration of students to seats is acceptable if the following conditions are met:
-
Each student is assigned to exactly one seat.
-
The students are seated in increasing order of height from left to right in each row.
The students are initially seated in an acceptable arrangement. Charlie wants to rearrange students into a potentially different acceptable arrangement. To do this, he can swap any two students. However, he wants to ensure that the configuration stays acceptable after each swap.
Help Charlie devise a strategy to move the students from the
original arrangement to his preferred arrangement. You don’t
need to minimize the number of swaps, but you are limited to at
most
It can be proven that this is always possible for all possible inputs that satisfy the input constraints.
Input
The first line of input contains two integers
Each of the next
Each of the next
Output
On the first line, output an integer
Then, output the
It can be proven that it’s always possible to accomplish
this in under
Sample Input 1 | Sample Output 1 |
---|---|
2 3 1 4 5 2 3 6 3 5 6 1 2 4 |
4 2 1 1 1 2 2 1 1 2 3 1 3 2 3 1 2 |