| Input | Output | |
| Test Case 1 | 4 10 1 10 2 100 2 104 3 | 10 4 14 9 |
| Test Case 2 | 3 923 8 23 9 101 2 | 10 5 14 |
| Test Case 3 | 5 42 8 35 1 70 5 79 9 63 5 | 6 35 7 7 7 |
| Test Case 4 | 10 42 8 335 1 170 5 479 9 963 5 706 6 282 8 962 2 996 3 828 7 | 6 335 8 9 11 10 9 44 19 10 |
| Test Case 5 | 20 42 8 335 1 170 5 479 9 963 5 706 6 282 8 962 2 996 3 828 7 392 5 903 4 293 3 422 7 719 6 448 7 772 9 870 3 668 10 36 5 | 6 335 8 9 11 10 9 44 19 10 10 13 12 9 10 9 10 18 10 6 |
| Test Case 6 | 100 42 8 335 1 170 5 479 9 963 5 706 6 282 8 962 2 996 3 828 7 392 5 903 4 293 3 422 7 719 6 448 7 772 9 870 3 668 10 36 5 704 2 323 4 674 5 142 2 254 9 548 5 663 8 38 10 724 2 530 9 317 6 191 3 289 7 41 3 265 9 447 6 891 10 371 1 7 2 394 9 630 4 85 5 757 1 967 7 932 9 945 10 627 4 538 9 119 3 930 2 834 6 640 9 705 1 978 7 674 7 22 6 925 3 271 10 778 4 98 3 987 1 162 7 356 8 656 5 32 3 351 1 942 5 967 1 108 2 8 8 458 8 754 4 946 10 210 9 222 9 423 7 507 1 414 9 901 2 763 6 411 10 625 8 549 4 596 2 603 1 292 7 375 1 597 2 349 10 669 5 282 5 54 10 419 9 901 9 128 8 729 4 649 4 808 2 311 8 814 5 | 6 335 8 9 11 10 9 44 19 10 10 13 12 9 10 9 10 18 10 6 38 10 11 17 8 10 10 6 38 10 9 11 9 6 9 9 10 371 4 9 12 7 757 10 10 10 12 10 9 43 10 10 705 11 10 5 18 9 12 9 987 8 9 11 6 351 11 967 15 4 9 12 10 8 8 9 507 9 42 10 9 10 11 35 603 9 375 35 9 11 9 6 9 10 8 12 12 40 9 11 |
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include <math.h> #define egg 10 #define floor 1000 /* *---------------------------------- * Function that returns the maximum of a and b *---------------------------------- */ int max(int a, int b){ if(a > b){ return a; } return b; } /* *---------------------------------- * Function that returns the minimum of a and b *---------------------------------- */ int minimum(int a, int b){ if(a < b){ return a; } return b; } int main(){ int matrix[floor+1][egg+1]; memset(matrix, 0, sizeof(matrix)); /* initialize matrix to 0 */ /* * We fill the initial values in the matrix * If there are 0 eggs, then we cannot solve the problem. * We set matrix[i][0] = INT_MAX to indicate that it takes * infinitely many queries */ for(int i=0; i<=floor; i++){ matrix[i][0] = INT_MAX; } /* * if there are 0 floors, then you need only 0 queries * if there is exactly 1 floor, then you need 1 query */ for(int i=0; i<=egg; i++){ matrix[0][i] = 0; matrix[1][i] = 1; } /* * if there is only 1 egg, then you need i queries to * determine the floor (starting from the top and going down, * stopping at the highest (first) floor where the egg * breaks. */ for(int i=0; i<=floor; i++){ matrix[i][1] = i; } int temp, min; /* *------------------------------------------------ * Now we come to the main recurrence: * * Suppose the total number of floors is "floor", and the * total number of eggs is "egg" * * We try to find the minimum number of eggs needed if the * highest floor is numbered i, and we have j eggs in our * hands. * * We can pick a floor k <= i and throw an egg from floor * k. There are two possibilities - * * 1. The egg breaks. In this case, the lowest floor at which * the egg can break is either k or lower, and you now have to * solve this problem with j-1 eggs. * * 2. The egg does not break. In this case, the lowest floor * at which the egg breaks is higher than k - that is, it is * among i-k floors, and you have j eggs to solve the problem. * * We don't know whether the egg will break or not. However, * we know that in the worst case, the number of eggs needed * if we try at floor k is the greater of 1 and 2. * cost(k) = max(1,2) * * The optimal way to solve the problem if the highest floor * is i, is to pick a k which minimizes the cost. * optcost(i) = min{cost(k) | 0 <= k <= i} *------------------------------------------------------- */ for(int j=2; j<=egg; j++){ for(int i=2; i<=floor; i++){ min = INT_MAX; for(int k=1; k<=i; k++){ temp = 1 + max(matrix[k-1][j-1], matrix[i-k][j]); min = minimum(min, temp); } matrix[i][j] = min; } } int query, n, k; scanf("%d", &n): while(query--){ scanf("%d%d",&n,&k); printf("%d\n", matrix[n][k]); } return 0; }
Labels: buildings, c program, dynamic programming, eggs, height, number of steps, source code