#P9662. [ICPC2021 Macao R] Cyclic Buffer
[ICPC2021 Macao R] Cyclic Buffer
题目描述
There is a cyclic buffer of size with readers from the -st position to the -th position (both inclusive). Let () be the integer at the -th position of the buffer initially. What's more, form a permutation of .
We're going to visit all the integers from to (both inclusive) in increasing order. An integer can be visited only when it is residing in positions with readers (that is to say, when it is in the first positions). In case that an integer cannot be visited, we can shift the whole buffer in either directions any number of times.
- If we shift the buffer to the left once, integers in the -th position will be moved to the -th position if , and integer in the -st position will be moved to the -th position.
- If we shift the buffer to the right once, integers in the -th position will be moved to the -th position if , and integer in the -th position will be moved to the -st position.
What's the minimum number of times to shift the buffer so that we can visit all the integers in increasing order?
输入格式
There are multiple test cases. The first line of the input contains an integer indicating the number of test cases. For each test case:
The first line contains two integers and () indicating the size of the buffer and the number of readers.
The second line contains integers () where indicates the integer in the -th position of the buffer initially.
It's guaranteed that the given integers form a permutation of . It's also guaranteed that the sum of of all test cases will not exceed .
输出格式
For each test case output one line containing one integer indicating the minimum number of times to shift the buffer so that all integers can be visited in increasing order.
2
5 3
2 4 3 5 1
1 1
1
3
0
提示
For the first sample test case:
- Shift to the right once so the buffer becomes . and can now be visited in order as they are in the first positions.
- Shift to the left twice so the buffer becomes . , and can now be visited in order as they are in the first positions.