# Untitled

unknown

plain_text

a year ago

7.5 kB

9

Indexable

Never

^{}

The Little Elephant and his friends from the Zoo were returning from the party. But suddenly they were stopped by the policeman Big Hippo, who wanted to make an alcohol test for elephants. There were N elephants ordered from the left to the right in a row and numbered from 0 to N-1.Let R[i] to be the result of breathalyzer test of i-th elephant. Considering current laws in the Zoo, elephants would be arrested if there exist K consecutive elephants among them for which at least M of these K elephants have the maximal test result among these K elephants. Using poor math notations we can alternatively define this as follows. The elephants would be arrested if there exists i from 0 to N-K, inclusive, such that for at least M different values of j from i to i+K-1,inclusive, we have R[j] = max{R[i], R[i+1], ..., R[i+K-1]}. The Big Hippo is very old and the Little Elephant can change some of the results. In a single operation he can add 1 to the result of any elephant. But for each of the elephants he can apply this operation at most once. What is the minimum number of operations that the Little Elephant needs to apply, such that the sequence of results, after all operations will be applied, let elephants to avoid the arrest? If it is impossible to avoid the arrest applying any number of operations, output -1. Input The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains three space-separated integers N,K, M. The second line contains N space-separated integers R[0],R[1], ..., R[N-1] denoting the test results of the elephants. Output For each test case, output a single line containing the minimum number of operations needed to avoid the arrest. Constraints 1 ≤ T ≤ 10 1 ≤ M ≤ K ≤ N ≤ 17 1 ≤ R[i] ≤ 17 Example Input: 4 5 3 2 1 3 1 2 1 5 3 3 7 7 7 7 7 5 3 3 7 7 7 8 8 4 3 1 1 3 1 2 Output: #1 0 #2 1 #3 1 #4 -1 Explanation Example case 1. Let's follow the poor math definition of arrest. We will consider all values of i from 0 to N-K = 2, inclusive, and should count the number of values of j described in the definition. If it less than M = 2 then this value of i does not cause the arrest, otherwise causes. i {R[i],...,R[i+K-1]} max{R[i],...,R[i+K-1]} For which j = i, ..., i+K-1 we have R[j] = max Conclusion i=0 {1, 3, 1} max = 3 R[j] = 3 for j = 1 does not cause the arrest i=1 {3, 1, 2} max = 3 R[j] = 3 for j = 1 does not cause the arrest i=2 {1, 2, 1} max = 2 R[j] = 2 for j = 3 does not cause the arrest So we see that initial test results of the elephants do not cause their arrest. Hence the Little Elephant does not need to apply any operations. Therefore, the answer is 0. Example case 2.Wehave N = 5, K = 3, M = 3. Let's construct similar table as in example case 1. Here the value of i will cause the arrest if we have at least 3 values of j described in the definition. i {R[i],...,R[i+K-1]} max{R[i],...,R[i+K-1]} For which j = i, ..., i+K-1 we have R[j] = max Conclusion i=0 {7, 7, 7} max = 7 R[j] = 7 for j = 0, 1, 2 causes the arrest i=1 {7, 7, 7} max = 7 R[j] = 7 for j = 1, 2, 3 causes the arrest i=2 {7, 7, 7} max = 7 R[j] = 7 for j = 2, 3, 4 causes the arrest So we see that for initial test results of the elephants each value of i causes their arrest. Hence the Little Elephant needs to apply some operations in order to avoid the arrest. He could achieve his goal by adding1 to the result R[2]. Then results will be {R[0], R[1], R[2], R[3],R[4]} = {7, 7, 8, 7, 7}. Let's check that now elephants will be not arrested. i {R[i],...,R[i+K-1]} max{R[i],...,R[i+K-1]} For which j = i, ..., i+K-1 we have R[j] = max Conclusion i=0 {7, 7, 8} max = 8 R[j] = 8 for j = 2 does not cause the arrest i=1 {7, 8, 7} max = 8 R[j] = 8 for j = 2 does not cause the arrest i=2 {8, 7, 7} max = 8 R[j] = 8 for j = 2 does not cause the arrest So we see that now test results of the elephants do not cause their arrest. Thus we see that using 0 operations we can't avoid the arrest but using 1 operation can. Hence the answer is 1. Example case 3.Wehave N = 5, K = 3, M = 3. Let's construct similar table as in example case 1. Here the value of i will cause the arrest if we have at least 3 values of j described in the definition. i {R[i],...,R[i+K-1]} max{R[i],...,R[i+K-1]} For which j = i, ..., i+K-1 we have R[j] = max Conclusion i=0 {7, 7, 7} max = 7 R[j] = 7 for j = 0, 1, 2 causes the arrest i=1 {7, 7, 8} max = 8 R[j] = 8 for j = 3 does not cause the arrest i=2 {7, 8, 8} max = 8 R[j] = 8 for j = 3, 4 does not cause the arrest So we see that for initial test results of the elephants the value of i =0 causes their arrest. Hence the Little Elephant needs to apply some operations in order to avoid the arrest. He could achieve his goal by adding1 to the result R[1]. Then results will be {R[0], R[1], R[2],R[3], R[4]} = {7, 8, 7, 8, 8}. Let's check that now elephants will be not arrested. i {R[i],...,R[i+K-1]} max{R[i],...,R[i+K-1]} For which j = i, ..., i+K-1 we have R[j] = max Conclusion i=0 {7, 8, 7} max = 8 R[j] = 8 for j = 1 does not cause the arrest i=1 {8, 7, 8} max = 8 R[j] = 8 for j = 1, 3 does not cause the arrest i=2 {7, 8, 8} max = 8 R[j] = 8 for j = 3, 4 does not cause the arrest So we see that now test results of the elephants do not cause their arrest. Thus we see that using 0 operations we can't avoid the arrest but using 1 operation can. Hence the answer is 1. Note that if we increase by 1 the result R[2] instead of R[1] then the value i = 2 will cause the arrest since {R[2], R[3], R[4]}will be {8, 8, 8} after this operation and we will have 3 values of j from 2 to 4,inclusive, for which R[j] = max{R[2], R[3], R[4]}, namely, j= 2, 3, 4. Example case 4. When M= 1 the Little Elephant can't reach the goal since for each value of i from 0 to N-K we have at least one value of j for which R[j] =max{R[i], R[i+1], ..., R[i+K-1]}. 50 5 3 2 1 3 1 2 1 5 3 3 7 7 7 7 7 5 3 3 7 7 7 8 8 4 3 1 1 3 1 2 9 3 2 16 4 6 1 9 9 5 2 1 7 4 4 2 10 10 10 10 10 3 9 3 3 5 1 1 7 1 1 1 1 1 7 2 2 10 2 9 17 12 11 4 17 14 14 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 7 13 2 2 2 5 5 14 9 6 7 6 11 14 6 7 7 7 7 7 7 7 7 7 4 2 1 3 8 15 9 6 3 3 15 15 15 15 3 7 1 1 1 1 5 2 2 14 14 14 7 5 8 2 2 4 16 4 4 4 4 4 4 6 2 2 2 15 17 17 17 17 8 3 2 5 8 17 16 16 16 7 7 15 9 9 10 10 10 10 10 10 10 10 10 10 10 10 10 16 3 7 6 5 9 3 14 12 12 12 10 3 3 3 6 3 17 11 11 2 4 5 4 10 16 12 10 3 3 10 10 2 2 2 3 3 4 2 2 1 16 8 8 5 4 2 17 3 8 13 13 17 7 6 3 3 3 3 3 3 3 3 3 3 3 15 11 6 8 10 6 17 5 5 17 5 5 5 5 5 5 5 5 5 5 12 12 12 12 12 15 8 3 2 11 7 6 6 6 13 14 8 1 1 1 2 2 2 1 12 9 7 6 3 1 1 1 7 14 7 13 9 8 7 12 14 16 5 7 1 6 3 17 5 2 2 2 6 6 6 6 15 5 5 16 3 6 17 17 17 17 17 17 17 17 17 17 17 17 17 5 5 17 9 8 4 2 7 7 16 10 7 14 12 6 5 3 6 1 3 2 2 10 10 10 17 5 5 4 4 4 4 4 4 4 4 12 6 2 9 8 13 10 9 2 14 6 5 2 5 9 13 4 8 12 16 4 14 17 6 5 9 6 6 2 6 2 16 16 16 16 5 3 2 10 11 9 2 4 15 5 4 7 7 7 7 7 7 7 7 7 2 6 5 14 1 14 14 4 3 15 3 3 3 3 3 3 3 3 3 5 3 14 3 9 4 3 13 13 13 13 13 13 11 4 16 7 3 3 1 7 2 2 2 8 3 5 4 3 13 8 1 13 1 11 4 2 13 13 12 14 2 9 8 10 7 3 9 10 8 8 7 7 7 7 7 7 7 7 7 7 5 3 1 7 6 4 16 16 10 5 5 3 6 6 6 6 6 6 6 1 14 4 2 2 12 12 12 16