Cyclic Buffer
题号:NC236068
时间限制:C/C++/Rust/Pascal 3秒,其他语言6秒
空间限制:C/C++/Rust/Pascal 512 M,其他语言1024 M
64bit IO Format: %lld

题目描述

There is a cyclic buffer of size n with readers from the 1-st position to the k-th position (both inclusive). Let a_i () be the integer at the i-th position of the buffer initially. What's more, form a permutation of n.
We're going to visit all the integers from 1 to n (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 k 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 i-th position will be moved to the (i - 1)-th position if , and integer in the 1-st position will be moved to the n-th position.
  • If we shift the buffer to the right once, integers in the i-th position will be moved to the -th position if , and integer in the n-th position will be moved to the 1-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 T indicating the number of test cases. For each test case:
The first line contains two integers n and k () indicating the size of the buffer and the number of readers.
The second line contains n integers () where a_i indicates the integer in the i-th position of the buffer initially.
It's guaranteed that the given n integers form a permutation of n. It's also guaranteed that the sum of n 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.
示例1

输入

复制
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 . 1 and 2 can now be visited in order as they are in the first 3 positions.Shift to the left twice so the buffer becomes . 3, 4 and 5 can now be visited in order as they are in the first 3 positions.