Google Kick Start Archive — Round B 2020 problems

Overview

Thank you for participating in Kick Start 2020 Round B.


Cast

Bike Tour: Written by Swante Scholz and prepared by Raihat Zaman Neloy.

Bus Routes: Written by Jin Park and prepared by Swapnil Gupta.

Robot Path Decoding: Written by Swante Scholz and prepared by Jonathan Irvin Gunawan.

Wandering Robot: Written by Yossi Matsumoto and prepared by Anson Ho.

Solutions, other problem preparation, reviews and contest monitoring by Ankit Goyal, Anson Ho, Bohdan Pryshchenko, Boon Eng Oh, Goutham Harsha, Jared Gillespie, Jonathan Irvin Gunawan, Kashish Bansal, Kevin Tran, Krists Boitmanis, Lalit Kundu, Lizzie Sapiro,Naranbayar Uuganbayar, Paul Hoang, Raihat Zaman Neloy, Ruoyu Zhang, Sadia Atique, Seunghyun Jo, Shantam Agarwal, Sudarsan Srinivasan, Swante Scholz, and Swapnil Gupta, Yuxin Wei.

Analysis authors:

  • Bike Tour: Sadia Atique
  • Bus Routes: Swapnil Gupta
  • Robot Path Decoding: Swapnil Gupta
  • Wandering Robot: Jonathan Irvin Gunawan

A. Bike Tour

Problem

Li has planned a bike tour through the mountains of Switzerland. His tour consists of N checkpoints, numbered from 1 to N in the order he will visit them. The i-th checkpoint has a height of Hi.

A checkpoint is a peak if:

  • It is not the 1st checkpoint or the N-th checkpoint, and
  • The height of the checkpoint is strictly greater than the checkpoint immediately before it and the checkpoint immediately after it.

Please help Li find out the number of peaks.

Input

The first line of the input gives the number of test cases, T. T test cases follow. Each test case begins with a line containing the integer N. The second line contains N integers. The i-th integer is Hi.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the number of peaks in Li's bike tour.

Limits

Time limit: 10 seconds.
Memory limit: 1 GB.
1 ≤ T ≤ 100.
1 ≤ Hi ≤ 100.

Test Set 1

3 ≤ N ≤ 5.

Test Set 2

3 ≤ N ≤ 100.

Sample

Sample Input
content_copy Copied!
4
3
10 20 14
4
7 7 7 7
5
10 90 20 90 10
3
10 3 10
Sample Output
content_copy Copied!
Case #1: 1
Case #2: 0
Case #3: 2
Case #4: 0

  • In sample case #1, the 2nd checkpoint is a peak.
  • In sample case #2, there are no peaks.
  • In sample case #3, the 2nd and 4th checkpoint are peaks.
  • In sample case #4, there are no peaks.

B. Bus Routes

Problem

Bucket is planning to make a very long journey across the countryside by bus. Her journey consists of N bus routes, numbered from 1 to N in the order she must take them. The buses themselves are very fast, but do not run often. The i-th bus route only runs every Xi days.

More specifically, she can only take the i-th bus on day Xi, 2Xi, 3Xi and so on. Since the buses are very fast, she can take multiple buses on the same day.

Bucket must finish her journey by day D, but she would like to start the journey as late as possible. What is the latest day she could take the first bus, and still finish her journey by day D?

It is guaranteed that it is possible for Bucket to finish her journey by day D.

Input

The first line of the input gives the number of test cases, T. T test cases follow. Each test case begins with a line containing the two integers N and D. Then, another line follows containing N integers, the i-th one is Xi.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the latest day she could take the first bus, and still finish her journey by day D.

Limits

Time limit: 10 seconds.
Memory limit: 1 GB.
1 ≤ T ≤ 100.
1 ≤ XiD.
1 ≤ N ≤ 1000.
It is guaranteed that it is possible for Bucket to finish her journey by day D.

Test Set 1

1 ≤ D ≤ 100.

Test Set 2

1 ≤ D ≤ 1012.

Sample

Sample Input
content_copy Copied!
3
3 10
3 7 2
4 100
11 10 5 50
1 1
1
Sample Output
content_copy Copied!
Case #1: 6
Case #2: 99
Case #3: 1

In Sample Case #1, there are N = 3 bus routes and Bucket must arrive by day D = 10. She could:

  • Take the 1st bus on day 6 (X1 = 3),
  • Take the 2nd bus on day 7 (X2 = 7) and
  • Take the 3rd bus on day 8 (X3 = 2).

In Sample Case #2, there are N = 4 bus routes and Bucket must arrive by day D = 100. She could:

  • Take the 1st bus on day 99 (X1 = 11),
  • Take the 2nd bus on day 100 (X2 = 10),
  • Take the 3rd bus on day 100 (X3 = 5) and
  • Take the 4th bus on day 100 (X4 = 50),

In Sample Case #3, there is N = 1 bus route and Bucket must arrive by day D = 1. She could:

  • Take the 1st bus on day 1 (X1 = 1).

C. Robot Path Decoding

Problem

Your country's space agency has just landed a rover on a new planet. The planet's surface can be thought of as a grid of squares containing 109 columns (numbered starting from 1 from west to east) and 109 rows (numbered starting from 1 from north to south). Let (w, h) denote the square in the w-th column and the h-th row. The rover begins on the square (1, 1).

The rover can be maneuvered around on the surface of the planet by sending it a program, which contains a string of characters representing movements in the four cardinal directions. The robot executes each character of the string in order. The rover moves according to the following rules:

  • N: Move one unit north.
  • S: Move one unit south.
  • E: Move one unit east.
  • W: Move one unit west.

There is also a special instruction X(Y), where X is a number between 2 and 9 (inclusive) and Y is a non-empty subprogram. This denotes that the robot should repeat the subprogram Y a total of X times. For example:

  • 2(NWE) is equivalent to NWENWE.
  • 3(S2(E)) is equivalent to SEESEESEE.
  • EEEE4(N)2(SS) is equivalent to EEEENNNNSSSS.

Since the planet is a spheroid, the first and last columns are adjacent, so moving east from column 109 will move the rover to column 1 and moving south from row 109 will move the rover to row 1. Similarly, moving west from column 1 will move the rover to column 109 and moving north from row 1 will move the rover to row 109. Given a program that the robot will execute, determine the final position of the robot after it has finished all its movements.

Input

The first line of the input gives the number of test cases, T. T lines follow. Each line contains a single string: the program sent to the rover.

Output

For each test case, output one line containing Case #x: w h, where x is the test case number (starting from 1) and w h is the final square (w, h) the rover finishes in.

Limits

Time limit: 10 seconds.
Memory limit: 1 GB.
1 ≤ T ≤ 100.
The string represents a valid program.
The length of each program is between 1 and 2000 characters inclusive.

Test Set 1

The total number of moves the robot will make in a single test case is at most 104.

Test Set 2

No additional constraints.

Sample

Sample Input
content_copy Copied!
4
SSSEEE
N
N3(S)N2(E)N
2(3(NW)2(W2(EE)W))
Sample Output
content_copy Copied!
Case #1: 4 4
Case #2: 1 1000000000
Case #3: 3 1
Case #4: 3 999999995

In Sample Case #1, the rover moves three units south, then three units east.

In Sample Case #2, the rover moves one unit north. Since the planet is a torus, this moves it into row 109.

In Sample Case #3, the program given to the rover is equivalent to NSSSNEEN.

In Sample Case #4, the program given to the rover is equivalent to NWNWNWWEEEEWWEEEEWNWNWNWWEEEEWWEEEEW.

D. Wandering Robot

Problem

Jemma is competing in a robotics competition. The challenge for today is to build a robot that can navigate around a hole in the arena.

The arena is a grid of squares containing W columns (numbered 1 to W from left to right) and H rows (numbered 1 to H from top to bottom). The square in the x-th column and y-th row is denoted (x, y). The robot begins in the top left square (1,1) and must navigate to the bottom right square (W, H).

A rectangular subgrid of squares has been cut out of the grid. More specifically, all the squares that are in the rectangle with top-left square (L, U) and bottom-right square (R, D) have been removed.

Jemma did not have much time to program her robot, so it follows a very simple algorithm:

  • If the robot is in the rightmost column, it will always move to the square directly below it. Otherwise,
  • If the robot is in the bottommost row, it will always move to the square directly right of it. Otherwise,
  • The robot will randomly choose to either move to the square directly to the right, or to the square directly below it with equal probability.

Jemma passes the challenge if her robot avoids falling into the hole and makes it to the square (W, H). What is the probability she passes the challenge?

Input

The first line of the input gives the number of test cases, T. T test cases follow. Each test case consists of a single line containing W, H, L, U, R and D.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is a real number between 0 and 1 inclusive, the probability that Jemma passes the challenge.

y will be considered correct if it is within an absolute or relative error of 10-5 of the correct answer. See the FAQ for an explanation of what that means, and what formats of real numbers we accept.

Limits

Time limit: 15 seconds.
Memory limit: 1 GB.
1 ≤ T ≤ 100.
1 ≤ UDH.
1 ≤ LRW.
Neither the top-left nor bottom-right squares will be missing.

Test Set 1

1 ≤ W ≤ 300.
1 ≤ H ≤ 300.

Test Set 2

1 ≤ W ≤ 105.
1 ≤ H ≤ 105.

Sample

Sample Input
content_copy Copied!
4
3 3 2 2 2 2
5 3 1 2 4 2
1 10 1 3 1 5
6 4 1 3 3 4
Sample Output
content_copy Copied!
Case #1: 0.5
Case #2: 0.0625
Case #3: 0.0
Case #4: 0.3125

Analysis — A. Bike Tour

For each of the checkpoints, we can determine if it is a peak in O(1) time by comparing its height to the heights of the checkpoints before and after it.

There are N checkpoints, so the total time complexity of this approach is O(N), which is sufficient for both Test Set 1 and Test Set 2.

Sample Code(C++)

int countPeaks(vector<int> checkpoints) {
  int peaks = 0;
  for(int i = 1; i < checkpoints.size() - 1; i++) {
     if(checkpoints[i-1] < checkpoints[i] && checkpoints[i+1] < checkpoints[i]) {
        peaks++;
     }
  }
  return peaks;
}
Test Data
info We recommend that you practice debugging solutions without looking at the test data.

Analysis — B. Bus Routes

We need to take the buses in order from 1 to N. Let the buses be B1, B2, ...., BN. Also, let us define a good starting day to be any day Y in the range [1..D] such that it is possible to start the journey on day Y and take all buses in the order from 1 to N before the end of day D. Note that we do not require Y to be a multiple of X1, so there may be some waiting time involved in the beginning of the journey.

For a fixed day Y, let us see if it is a good starting day or not. The best strategy would be to take bus B1 as early as possible on or after day Y. This is because it would give us more days to take subsequent buses. Let us say we took bus B1 on day D1. Now the best strategy would be to take bus B2 as early as possible on or after day D1. Thus, if we took bus Bi on day Di, it would be optimal to take bus Bi+1 as early as possible on or after day Di.

Now we need to find out what is the earliest possible day for Bucket to take bus Bi on or after a particular day K. Since bus Bi only runs on days that are multiples of Xi, we need to find the smallest multiple of Xi greater than or equal to K. This can be calculated using the formula ⌈ K / Xi ⌉ * Xi. Thus if bus Bi is taken on day Di, then it would be optimal to take bus Bi+1 on day Di+1 = ⌈ Di / Xi+1 ⌉ * Xi+1. Thus, day Y is a good starting day if DND, and this question can be answered in O(N) time.

Test set 1

D can be at most 100, so we can find the latest good starting day by using the above approach for each day Y in the range [1..D]. The time complexity of this naive algorithm is O(DN).

Test set 2

Now D can be at most 1012, so the naive algorithm would time out. Consider the largest good starting day P. Obviously, any day before P would be good as well because we can take the buses on the same days as if we started the journey on day P. Because of this observation, we can binary search on the range from 1 to D to find the largest good starting day P. The time complexity of the solution is O(N log D). Note that we can reduce the time complexity to O(N log(D/X1)) by restricting the search to multiples of X1 only.

Alternate solution

It is possible to solve the problem in linear time by working out the solution backwards. If we want to start our journey as late as possible, we should try to take the last bus BN as late a possible, namely, on day DN, which is the largest multiple of XN, less than or equal to day D. Similarly, in order to be on time for the last bus on day DN, we have to take bus BN-1 no later than on day DN-1, which is the largest multiple of XN-1, less than or equal to DN. In general, bus Bi should be taken no later than on day Di, which is the largest multiple of Xi, less than or equal to Di+1. The last calculated value D1 is the answer to the problem.

Note that the largest multiple of Xi that occurs before a day L can be calculated in constant time as L - L mod Xi. Therefore, the overall time complexity of this solution is O(N).

Test Data
info We recommend that you practice debugging solutions without looking at the test data.

Analysis — C. Robot Path Decoding

To facilitate parsing of the program, let us define ClosingBracket(i) as the index of the closing bracket corresponding to the opening bracket at index i. We can find ClosingBracket(i) for each opening bracket using a stack in linear time, which is similar to checking whether a string is a correct bracket sequence or not, see this article for more details.

Test Set 1

The total number of moves is limited by 104 per test. Consider the expanded version of a program P to be the string consisting of characters N,S,W,E only and having the same moves as P. For example, the program 2(3(N)EW) would expand to NNNEWNNNEW. Since the number of moves is small, we can generate the expanded program, and calculate the position of the robot easily by taking one step at a time.

For an original subprogram between indices L and R, the equivalent expanded version Expanded(L, R) can be constructed recursively as follows. We start with an empty string Result, iterate the subprogram from the left index L to the right index R, and consider two cases:

  • If the i-th symbol is in {'N','S','E','W'}, append it to Result.
  • If the i-th symbol is a digit D then
    • Call Expanded(i+2, ClosingBracket(i+1)-1) to construct the expanded version P' of the next subprogram recursively,
    • Append P' to Result D times, and
    • Advance the current position i to ClosingBracket(i+1)+1.

The first case takes constant time. In the second case, it takes O(D × |P'|) time to append the subprogram P' to the result D times. Let LEN be the length of the expanded program. The total expanded length of the subprograms at the second nesting level is at most LEN/2. The total expanded length of subprograms at the third nesting level is at most LEN/4, and so on. Thus the time complexity would be bounded by LEN + LEN / 2 + LEN / 4 + LEN / 8 + .. which is equal to 2 × LEN as this is a geometric progression. Hence, the time complexity to generate the expanded version of the original program would be O(LEN).

Test Set 2

Now it is possible that the number of moves is exponential in the length of the original program. Thus it would be impossible to execute the moves one by one in the given time.

For the ease of explanation, let us assume that the rows and columns are numbered from 0 (inclusive) to 109 (exclusive). Suppose that the robot is at position (a, b) and now we come across instruction X(Y) in the program. Let us say subprogram Y changes the current position of the robot by dx, dy (because of the torus shape of Mars, we can assume that 0 ≤ dx < 109 and 0 ≤ dy < 109). Then the position of the robot after following the instruction X(Y) would be ((a + X * dx) mod 109, (b + X * dy) mod 109) as the subprogram Y is repeated X times. Hence, we just need to find the relative displacement of the robot by each subprogram.

For a subprogram between indices L and R, the relative displacement of the robot can be calculated using Evaluate(L, R) recursively as follows. Consider that we are currently at the square (a, b), which is initially the square (0, 0). Iterate the subprogram from the left index L to the right index R, and consider two cases:

  • If the i-th symbol is in {'N','S','E','W'}, change the current position of the robot accordingly.
  • If the i-th symbol is a digit D then
    • Call Evaluate(i+2, ClosingBracket(i+1)-1)to get the relative displacement (dx, dy) of the robot by the next subprogram recursively,
    • Change the current position to ((a + D * dx) mod 109, (b + D * dy) mod 109), and
    • Advance the current position i to ClosingBracket(i+1)+1.

Clearly, we visit each character in the program exactly once. Hence, the time complexity of the solution is O(N), where N is the length of the program.

Test Data
info We recommend that you practice debugging solutions without looking at the test data.

Analysis — D. Wandering Robot

Test Set 1

We can solve this test set using dynamic programming. Let f(x, y) be the probability Jemma passes the challenge if she is currently in the square (x, y). The base case of this function is f(W, H) = 1. Also, if the square (x, y) has been removed, then f(x, y) = 0.

If there is only one possible square to go to from square (x, y) (i.e. either x = W or y = H), then f(x, y) = f(x', y'), where (x', y') is the possible next square. Otherwise, let (x1', y1') and (x2', y2') be the possible next squares. Since they have the same probability to become the next square, f(x, y) = 0.5 × f(x1', y1') + 0.5 × f(x2', y2').

The running time and space of this solution is O(W × H).

Test Set 2

The first observation to solve this problem is to realize that there are two ways to avoid the hole: either going to the left and the bottom of the hole (illustrated by the red path in the figure below), or going to the top and the right of the hole (illustrated by the blue path in the figure below).

It can be seen that the set of paths in the red path and the blue path are disjoint--there is no path that goes both to the left of the hole and to the top of the hole simultaneously. Therefore, we can compute the probability that Jemma passes the challenge by taking the red path and the blue path separately and compute the sum of both probabilities.

Since the probability of passing the challenge by taking the blue path can be computed similarly, we only focus on computing the probability of passing the challenge by taking the red path for the rest of the discussion. The next observation to solve this problem is that we can choose a set of squares diagonally from the bottom-left corner of the hole (illustrated by the green squares below) such that Jemma has to pass exactly one of the squares to pass the challenge by taking the red path. Also, by landing on one of the squares, it is no longer possible that Jemma will fall to the hole, thus passing the challenge by taking the red path is now guaranteed.

Therefore, computing the probability of passing the challenge by taking the red path is equivalent to computing the probability that Jemma will land on one of the green squares. Similar to the red and blue paths discussion, since Jemma cannot pass two green squares simultaneously, we can compute the probability that Jemma lands on each square separately and compute the sum of all probabilities.

Let us take square X for an example. Consider all paths that go to the square X. For each move in the path, there is a 0.5 probability that the move will follow the path. Since the number of moves to square X is (L + D - 2), there is a (0.5)(L + D - 2) probability that this path will be taken. This number has to be multiplied with the number of paths to go to square X, which can be computed using a single binomial coefficient. The probability of reaching any particular green square is the same for all but the green square in the last row, which is left to the reader as an exercise.

To handle floating point issues, we can store every huge number in their log representation (i.e. storing log2(x) instead of x). We can then compute the value of C(n, k) / 2n using 2log2(n! / (k! × (n - k)!) / 2n) = 2log2(n!) - log2(k!) - log2((n - k)!) - n, which takes constant time to compute if we have precomputed every value of log2(x!). Since there can be at most O(N) green squares, where N is the larger length of the grid (i.e. N = max(H, W)), the total running time of this solution is O(N).

Test Data
info We recommend that you practice debugging solutions without looking at the test data.

Statistics — A. Bike Tour

Statistics — B. Bus Routes

Statistics — C. Robot Path Decoding

Statistics — D. Wandering Robot