- ==========
- Checkpoint
- ==========
- You are racing on a 2D lattice grid starting from the origin (0,0) towards a goal (M,N) where M and N are positive integers such that 0 < M <= N. There is a checkpoint that's neither on the origin nor on the goal with coordinates (m,n) such that 0 <= m <= M and 0 <= n <= N. You must clear the checkpoint before you reach the goal. The shortest path takes T = M + N steps.
- At each point, you can move to the four immediate neighbors at a fixed speed, but since you don't want to lose the race, you are only going to take either a step to the right or to the top.
- Even though there are many ways to reach the goal while clearing the checkpoint, the race is completely pointless since it is relatively easy to figure out the shortest route. To make the race more interesting, we change the rules. Instead of racing to the same goal (M,N), the racers get to pick a goal (x,y) and place the checkpoint to their liking so that there are exactly S distinct shortest paths.
- For example, given S = 4, consider the following two goal and checkpoint configurations
- Placing the checkpoint at (1, 3) and the goal at (2,3). There are 4 ways to get from the origin to the checkpoint depending on when you move to the right. Once you are at the checkpoint, there is only one way to reach the goal with minimal number of steps. This gives a total of 4 distinct shortest paths, and takes T = 2 + 3 = 5 steps. However, you can do better.
- Placing the checkpoint at (1, 1) and the goal at (2,2). There are two ways to get from the origin to the checkpoint depending on whether you move to the right first or later. Similarly, there are two ways to get to the goal, which gives a total of 4 distinct shortest paths. This time, you only need T = 2 + 2 = 4 steps.
- As a Hacker Cup racer, you want to figure out how to place the checkpoint and the goal so that you cannot possibly lose. Given S, find the smallest possible T, over all possible checkpoint and goal configurations, such that there are exactly S distinct shortest paths clearing the checkpoint.
- Input
- As input for the race you will receive a text file containing an integer R, the number of races you will participate in. This will be followed by R lines, each describing a race by a single number S. Lines are separated using Unix-style ("\n") line endings.
- Output
- Your submission should contain the smallest possible length of the shortest path, T for each corresponding race, one per line and in order.
- Constraints
- 5 <= R <= 20
- 1 <= S <= 10000000
- Example input:
- 5
- 4
- 5
- 12
- 14
- 1
- Example output:
- Case #1: 4
- Case #2: 6
- Case #3: 6
- Case #4: 9
- Case #5: 2
- ====================
- Recover the Sequence
- ====================
- Merge sort is one of the classic sorting algorithms. It divides the input array into two halves, recursively sorts each half, then merges the two sorted halves.
- In this problem merge sort is used to sort an array of integers in ascending order. The exact behavior is given by the following pseudo-code:
- function merge_sort(arr):
- n = arr.length()
- if n <= 1:
- return arr
- // arr is indexed 0 through n-1, inclusive
- mid = floor(n/2)
- first_half = merge_sort(arr[0..mid-1])
- second_half = merge_sort(arr[mid..n-1])
- return merge(first_half, second_half)
- function merge(arr1, arr2):
- result = []
- while arr1.length() > 0 and arr2.length() > 0:
- if arr1[0] < arr2[0]:
- print '1' // for debugging
- result.append(arr1[0])
- arr1.remove_first()
- else:
- print '2' // for debugging
- result.append(arr2[0])
- arr2.remove_first()
- result.append(arr1)
- result.append(arr2)
- return result
- A very important permutation of the integers 1 through N was lost to a hard drive failure. Luckily, that sequence had been sorted by the above algorithm and the debug sequence of 1s and 2s was recorded on a different disk. You will be given the length N of the original sequence, and the debug sequence. Recover the original sequence of integers.
- Input
- The first line of the input file contains an integer T. This is followed by T test cases, each of which has two lines. The first line of each test case contains the length of the original sequence, N. The second line contains a string of 1s and 2s, the debug sequence produced by merge sort while sorting the original sequence. Lines are separated using Unix-style ("\n") line endings.
- Output
- To avoid having to upload the entire original sequence, output an integer checksum of the original sequence, calculated by the following algorithm:
- function checksum(arr):
- result = 1
- for i=0 to arr.length()-1:
- result = (31 * result + arr[i]) mod 1000003
- return result
- Constraints
- 5 ≤ T ≤ 20
- 2 ≤ N ≤ 10000
- Examples
- In the first example, N is 2 and the debug sequence is 1. The original sequence was 1 2 or 2 1. The debug sequence tells us that the first number was smaller than the second so we know the sequence was 1 2. The checksum is 994.
- In the second example, N is 2 and the debug sequence is 2. This time the original sequence is 2 1.
- In the third example, N is 4 and the debug sequence is 12212. The original sequence is 2 4 3 1.
- Example input:
- 5
- 2
- 1
- 2
- 2
- 4
- 12212
- 5
- 1122211
- 10
- 121221212111122121212
- Example output:
- Case #1: 994
- Case #2: 1024
- Case #3: 987041
- Case #4: 570316
- Case #5: 940812
- ===============
- Squished Status
- ===============
- Some engineers got tired of dealing with all the different ways of encoding status messages, so they decided to invent their own. In their new scheme, an encoded status message consists of a sequence of integers representing the characters in the message, separated by spaces. Each integer is between 1 and M, inclusive. The integers do not have leading zeroes. Unfortunately they decided to compress the encoded status messages by removing all the spaces!
- Your task is to figure out how many different encoded status messages a given compressed status message could have originally been. Because this number can be very large, you should return the answer modulo 4207849484 (0xfaceb00c in hex).
- For example, if the compressed status message is "12" it might have originally been "1 2", or it might have originally been "12". The compressed status messages are between 1 and 1000 characters long, inclusive. Due to database corruption, a compressed status may contain sequences of digits that could not result from removing the spaces in an encoded status message.
- Input
- The input begins with a single integer, N, the number of compressed status messages you must analyze. This will be followed by N compressed status messages, each consisting of an integer M, the highest character code for that database, then the compressed status message, which will be a string of digits each in the range '0' to '9', inclusive. All tokens in the input will be separated by some whitespace.
- Output
- For each of the test cases numbered in order from 1 to N, output "Case #i: " followed by a single integer containing the number of different encoded status messages that could be represented by the corresponding compressed sequence modulo 4207849484. If none are possible, output a 0.
- Constraints
- 5 <= N <= 25
- 2 <= M <= 255
- 1 <= length of encoded status <= 1000
- Example input:
- 5
- 12
- 12
- 255
- 219
- 30
- 1234321
- 2
- 101
- 70 8675309
- Example output:
- Case #1: 2
- Case #2: 4
- Case #3: 6
- Case #4: 0
- Case #5: 2