Connection Status:
Competition Arena > FroggerAndNets
SRM 833 · 2022-07-08 · by misof · Dynamic Programming, Greedy
Class Name: FroggerAndNets
Return Type: int
Method Name: jump
Arg Types: (string, int, int, int)
Problem Statement

Problem Statement

Frogger is a frog who really wants to survive. He also likes to jump.


There is a river. The river is polluted and jumping into it kills Frogger immediately.

In one place there is a ford across the river. At the ford the river is N steps wide. The ford is described by the N-character String stones. The characters of stones are numbered from 0 to N-1. Each character is either '=' (representing river) or 'O' (uppercase oh, representing a stone). The index of each 'O' character is the coordinate of the corresponding stone.

Frogger can stand on the stones. He's an excellent jumper: he can get from any stone to any other stone in a single jump.


Some bad people with nets are trying to catch Frogger. These bad people are afraid to enter the ford, so they remain on the banks of the river.

There will be a sequence exactly C catching attempts. These are numbered from 0 to C-1. Each catching attempt i has two parameters L[i] and R[i] with the following meaning: if Frogger's coordinate is strictly less than L[i] or strictly greater than R[i], he gets tangled into the nets. The bad people will then cook and eat him.

Frogger has observed the bad people's strategy and so he knows all the values L[i] and R[i] in advance.


Frogger can choose any of the stones as his starting location. Once he does so, catching attempt 0 takes place.

Between any two consecutive catching attempts is enough time for Frogger to make exactly one leap. (Remember that he can jump to any other stone. He is also allowed not to jump and simply remain on the same stone.) The length of a jump is the positive difference between the coordinates of the stones where it starts and ends.

Frogger's primary goal is to survive all catching attempts. If he cannot reach this goal, return -1.

Frogger's secondary goal is to jump as much as possible. If he can survive, return the maximum total length of his (at most) C-1 jumps.


In order to keep the input size small, the arrays L and R are generated pseudorandomly, so that each interval L[i], R[i] has width at least minW. Please use the following pseudocode.


state = seed
N     = length(stones)
maxW  = N-1

for i = 0 to C-1:
    state = (state * 1103515245 + 12345) modulo 2^31
    w = minW + (state modulo (maxW - minW + 1))
    state = (state * 1103515245 + 12345) modulo 2^31
    L[i] = state modulo (N - w)
    R[i] = L[i] + w	

Constraints

  • stones will contain between 1 and 2,000 characters, inclusive.
  • Each character in stones will be '=' or 'O'.
  • At least one character in stones will be 'O'.
  • C will be between 1 and 1,000,000, inclusive.
  • minW will be between 0 and N-1, inclusive, where N is the number of characters in stones.
  • seed will be between 0 and 2^31 - 1, inclusive.
Examples
0)
"=O=========O=="
11
13
4747
Returns: 100

With minW = N-1 = 13 each L[i] = 0 and each R[i] = N-1: the entire ford is safe during each catching attempt. As C = 11, there are 11 catching attempts. Frogger jumps once between each pair of attempts, so he will make 10 jumps. In the optimal solution Frogger starts on either stone and then always jumps to the other stone. The length of each jump is 10, so the total length of his jumps is 10*10 = 100.

1)
"=O=O=O=O=O=O="
10
2
4747
Returns: 64

The generated arrays are L = {7, 3, 1, 3, 1, 0, 8, 5, 6, 7} and R = {11, 12, 12, 7, 10, 8, 12, 8, 12, 12}. Below we show a visualization of the catching attempts. Time flows from top to bottom. In each row nets ('#') show the unsafe part of the river. =O=O=O=O=O=O= #######O=O=O# ###O=O=O=O=O= #O=O=O=O=O=O= ###O=O=O##### #O=O=O=O=O=## =O=O=O=O=#### ########=O=O= #####O=O=#### ######=O=O=O= #######O=O=O= One optimal solution starts as follows: Frogger chooses the rightmost stone (index 11). Catching attempt 0 happens. During the attempt Frogger is in the interval [7,11] so he avoids capture. Frogger jumps four stones to the left (to index 3 => jump length 8). Catching attempt 1 happens. During the attempt Frogger is in the interval [3,12] so he avoids capture.

2)
"=O=O=====O=O="
10
2
4747
Returns: -1

This example is very similar to the previous one, the only difference is the two missing stones. Due to the missing stones, catching attempt number 7 (the one with L[7] = 5 and R[7] = 8) will be successful: regardless of which stone Frogger chooses, the nets will get him.

3)
"=====O======="
123456
9
4242
Returns: 0

There is only one stone. On one hand it's always safe for Frogger to sit there (the nets never reach the stone), on the other he won't do much jumping in this test case.

4)
"==O=O==O==O=O==O===OO===O==O=OO=="
100
5
4747
Returns: 1798

Submissions are judged against all 82 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class FroggerAndNets with a public method int jump(string stones, int C, int minW, int seed) · 82 test cases · 2 s / 256 MB per case

Submitting as anonymous