Little Elephants
Level 4 Little Elephants Level 4 Little Elephants 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=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=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=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=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=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]}. #1 0 #2 1 #3 1 #4 -1 #5 1 #6 1 #7 1 #8 0 #9 1 #10 1 #11 1 #12 -1 #13 1 #14 -1 #15 1 #16 3 #17 2 #18 1 #19 1 #20 0 #21 0 #22 0 #23 1 #24 1 #25 1 #26 1 #27 3 #28 1 #29 -1 #30 -1 #31 0 #32 0 #33 2 #34 2 #35 0 #36 1 #37 1 #38 0 #39 1 #40 0 #41 2 #42 2 #43 1 #44 1 #45 0 #46 0 #47 1 #48 -1 #49 1 #50 1 Time: 0.063000000 s. 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 #include <iostream> #include <time.h> using namespace std; int oo = 2000000000; int T, n, k, m, result; int arr[18]; int maxx(int a, int b) { return (a>b ? a : b); } int minn(int a, int b) { return (a<b ? a : b); } bool checkCause(int index){ if(index < n - k) return false; for(int i = 0; i <= index - k; i++){ int maxx = -oo, cnt = 0; for(int j = i; j < i+k; j++){ if(arr[j] > maxx) maxx = arr[j]; } for(int j = i; j < i+k; j++){ if(arr[j] == maxx) cnt++; if(cnt == m) return true; } } return false; } void backtrack(int index, int cnt){ if(cnt >= result) return; if(checkCause(index)) return; if(index == n){ if(cnt < result) result = cnt; return; } for(int choose = 0; choose < 2; choose++){ bool valid; if(choose){ arr[index]++; backtrack(index + 1, cnt + 1); arr[index]--; } else{ backtrack(index + 1, cnt); } } } int main(){ freopen("input.txt", "r", stdin); // Calc clock clock_t time_start, time_end; time_start = clock(); cin >> T; for(int tc = 1; tc <= T; tc++){ // Initial && Input cin >> n >> k >> m; for(int i = 0; i < n; i++) cin >> arr[i]; result = oo; // Solve Problem backtrack(0, 0); // Output cout << "#" << tc << " " << (result == oo ? -1 : result) << endl; } // Calc Time time_end = clock(); cout.setf(ios::fixed); cout.precision(9); cout << "Time: " << double (time_end - time_start) / double (CLOCKS_PER_SEC) << " s." << endl; return 0; }
Leave a Comment