Guest

Untitled

By: a guest on Jan 28th, 2012  |  syntax: None  |  size: 6.83 KB  |  hits: 148  |  expires: in 4 days
download  |  raw  |  embed  |  report abuse
Copied
  1. 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.
  2. 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.
  3.  
  4. 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.
  5.  
  6. For example, given S = 4, consider the following two goal and checkpoint configurations
  7. 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.
  8. 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.
  9. 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.
  10.  
  11. Input
  12. 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.
  13.  
  14. Output
  15. Your submission should contain the smallest possible length of the shortest path, T for each corresponding race, one per line and in order.
  16.  
  17. Constraints
  18. R = 20
  19. 1 <= S <= 10000000
  20. Example inputExample output
  21. 5
  22. 4
  23. 5
  24. 12
  25. 14
  26. 1
  27. Case #1: 4
  28. Case #2: 6
  29. Case #3: 6
  30. Case #4: 9
  31. Case #5: 2
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40. Recover the Sequence
  41.  
  42. 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.
  43.  
  44. 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:
  45.  
  46. function merge_sort(arr):
  47.     n = arr.length()
  48.     if n <= 1:
  49.         return arr
  50.  
  51.     // arr is indexed 0 through n-1, inclusive
  52.     mid = floor(n/2)
  53.    
  54.     first_half = merge_sort(arr[0..mid-1])
  55.     second_half = merge_sort(arr[mid..n-1])
  56.     return merge(first_half, second_half)
  57.  
  58. function merge(arr1, arr2):
  59.     result = []
  60.     while arr1.length() > 0 and arr2.length() > 0:
  61.         if arr1[0] < arr2[0]:
  62.             print '1' // for debugging
  63.             result.append(arr1[0])
  64.             arr1.remove_first()
  65.         else:
  66.             print '2' // for debugging
  67.             result.append(arr2[0])
  68.             arr2.remove_first()
  69.            
  70.     result.append(arr1)
  71.     result.append(arr2)
  72.     return result
  73. 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.
  74.  
  75. Input
  76. 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.
  77.  
  78. Output
  79. To avoid having to upload the entire original sequence, output an integer checksum of the original sequence, calculated by the following algorithm:
  80.  
  81. function checksum(arr):
  82.     result = 1
  83.     for i=0 to arr.length()-1:
  84.         result = (31 * result + arr[i]) mod 1000003
  85.     return result
  86. Constraints
  87. 2 ≤ N ≤ 10000
  88.  
  89. Examples
  90. 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.
  91.  
  92. In the second example, N is 2 and the debug sequence is 2. This time the original sequence is 2 1.
  93.  
  94. In the third example, N is 4 and the debug sequence is 12212. The original sequence is 2 4 3 1.
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102. Squished Status
  103.  
  104. 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. Unfortunately they decided to compress the encoded status messages by removing all the spaces!
  105.  
  106. 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).
  107.  
  108. 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.
  109.  
  110. Input
  111. 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. All tokens in the input will be separated by some whitespace.
  112.  
  113. Output
  114. 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. If none are possible, output a 0.
  115.  
  116. Constraints
  117. 10 <= N <= 25
  118. 2 <= M <= 255
  119. 1 <= length of encoded status <= 1000
  120.  
  121. Example inputExample output
  122. 5
  123. 12
  124. 12
  125. 255
  126. 219
  127. 30
  128. 1234321
  129. 2
  130. 101
  131. 70 8675309
  132. Case #1: 2
  133. Case #2: 4
  134. Case #3: 6
  135. Case #4: 0
  136. Case #5: 2