Browsing the Collection
Time limit4sMemory limit512 MB
For every pair of items on a circle, find the fewest clicks and filter changes needed to move the pointer from one to the other.
- Level
Hard8 of 10
- Topics
- Shortest path, Graph, BFS
- Solved
- No attempts yet
Problem
You are browsing an online collection of items numbered from to arranged on a circle. The item to the right of each item is item , and the item to the right of item is item . Similarly, the item to the left of each item is item , and the item to the left of item is item .
The items have parameters numbered from to . The value of parameter for item is an integer .
While you are browsing, the pointer is always directed at some item, called the current item. You can also manage a set of filtering conditions. Each condition is a pair , meaning that the -th parameter of the item must be equal to . The current item always satisfies all conditions in the set.
To browse the collection, you perform operations. Each operation must be one of the following four kinds:
- Click right. The pointer moves to the closest item to the right of the current item that satisfies all filtering conditions. If the current item is the only such item, the pointer does not move.
- Click left. The pointer moves to the closest item to the left of the current item that satisfies all filtering conditions. If the current item is the only such item, the pointer does not move.
- Add a new filtering condition . If the current item satisfies this condition, the pointer does not move. Otherwise, the pointer moves to the closest item to the right of the current item that satisfies all filtering conditions, including the new one. If there is no such item, the operation is illegal and cannot be performed.
- Remove any filtering condition from the set. The pointer does not move.
For each ordered pair of items , answer the following question: if you start browsing with the pointer at item and with no filtering conditions in the set, what is the smallest number of operations needed to move the pointer to item ? The set of filtering conditions may be arbitrary at the end.
Input
The first line contains two integers and , the number of items and the number of parameters per item (; ).
The -th of the next lines contains integers , the parameter values of item ().
Output
Print lines with integers each. In the -th line, the -th integer is the smallest number of operations required to move the pointer from item to item , starting with an empty set of filtering conditions.
Hint
In the example test, one fastest way to move from item to item is:
- Add the filtering condition . Item has parameter equal to , so the pointer stays at item .
- Click right. The pointer moves to the closest item to the right of item that satisfies the only active condition . This item is item . (Clicking left works as well.)
One fastest way to move from item to item is:
- Add the filtering condition . Item does not satisfy it, so the pointer moves to the closest item to the right of item with parameter equal to . This item is item .
- Remove the filtering condition . The pointer stays at item .
- Click right. With no filtering conditions, the pointer moves to item .