Little Elephant and Alcohol

All submissions for this problem are available.
The Little Elephant and his friends from the Zoo of Lviv 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 N1. Let R[i] to be the result of breathalyzer test of ith elephant.
Considering current laws in the Zoo, elephants would be arrested if there exists 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 NK, inclusive, such that for at least M different values of j from i to i+K1, inclusive, we have R[j] = max{R[i], R[i+1], ..., R[i+K1]}.
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 spaceseparated integers N, K, M. The second line contains N spaceseparated integers R[0], R[1], ..., R[N1] 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: 0 1 1 1
Explanation
Example case 1. Let's follow the poor math definition of arrest. We will consider all values of i from 0 to NK = 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+K1]}  max{R[i],...,R[i+K1]}  For which j = i, ..., i+K1 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.We have 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+K1]}  max{R[i],...,R[i+K1]}  For which j = i, ..., i+K1 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 adding 1 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+K1]}  max{R[i],...,R[i+K1]}  For which j = i, ..., i+K1 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.We have 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+K1]}  max{R[i],...,R[i+K1]}  For which j = i, ..., i+K1 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 adding 1 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+K1]}  max{R[i],...,R[i+K1]}  For which j = i, ..., i+K1 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 NK we have at least one value of j for which R[j] = max{R[i], R[i+1], ..., R[i+K1]}.
Author:  witua 
Tester:  anton_lunyov 
Editorial  http://discuss.codechef.com/problems/LEALCO 
Tags  backtracking, bitmasking, easy, jan13, witua 
Date Added:  20032012 
Time Limit:  1 sec 
Source Limit:  50000 Bytes 
Languages:  C, JAVA, PYTH, PYTH 3.6, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, PERL6, TEXT, CLOJ, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions