You have a grid with
rows and columns in whichcells are black and the rest are white.
Let
represent the cell in the -th row from the top and the-th column from the left.
If you are currently at cell
, you can:
- move one cell down to
The cost of a path is defined as the number of times you move between two cells that have different colours.
You are given
queries. In the -th query, you are initially at and want to reachwith the minimum cost.
For each query, before you begin, you can perform the following operation any number of times (possibly zero):
- Choose a column and flip the colour of all cells in this column (i.e. change the colour of all the black cells to white and the colour of all the white cells to black in this column)
For each query, what is the minimum cost of a path from
toafter performing this operation any number of times (possibly zero)?
Note: The operations applied in different queries are independent of each other.
Input Format
- The first line of the input contains a single integer
- -th query.
Output Format
For each test case, print a single line containing
space separated integers ― the answer for each of thequeries.
Constraints
Sample Input 1
5
1 1 1 1
1 1
1 1
3 1 2 2
1 1
3 1
2 1
3 1
3 4 3 3
1 2
2 1
1 4
1 4
3 4
2 1
4 3 5 2
2 1
2 2
2 3
3 2
4 2
4 1
4 2
6 3 5 2
2 1
3 2
3 3
5 2
5 3
3 3
6 3
Sample Output 1
0
1 2
0 0 1
2 1
1 2
Explanation
Test case 1:
- Query 1: We are already at
- .
Test case 2:
- Initial Grid:
Query 1: The only sequence of possible moves is
.
Query 2: The only sequence of possible moves is
.
It can be shown that no better answer is possible by performing any column flipping operations.
Test case 3:
- Initial Grid:
- Query 1: One optimal solution is to flip columns
- to obtain the following grid.
Then you can perform the following moves for a cost of
-- Query 2: One optimal solution is to flip columns
- .
Then you can perform the following moves for a cost of
-- Query 3: One optimal solution is to flip no columns and then perform the following moves for a cost of
- .
Test case 4:
- Initial Grid:
Query 1: One optimal solution is to flip no columns and then perform the following moves for a cost of
Query 2: One optimal solution is to flip no columns and then perform the following moves for a cost of
ConversionConversion EmoticonEmoticon