SRM 814, D1, 250-Pointer (StepLeapSurviveTraps)

James S. Plank

Thu Oct 7 17:00:37 EDT 2021

In case topcoder's servers are down

Here's a summary of the problem: The code to generate T is as follows:

  T.push_back(0);
  state = seed;                    // state is declared as a long long
  while (T.size() <= N) {
    state *= 1103515245LL;
    state += 12345LL;
    state &= 0x7fffffff;
    T.push_back((state % M) + 1);
  }


Examples:

The examples suck. The only one that matters is example 0: N=8, J=3, seed=47, M=10, which yields the following for T: {0, 9, 6, 9, 2, 9, 4, 1, 8}. The answer here is 17:
i                0   2   4   5   6
T[i]             0   6   2   1   8
running score    0   6   8   9  17
At the bottom of this page, I'll show some outputs for other values, in case you want to check yourself, especially generating T.

Testing yourself

I have a shell script tests.sh, that you can use to test your program. When you run tests.sh, your answer should be identical to answers.txt.

Hints - Why Dijkstra's Algorithm Isn't Likely To Work

(If you haven't taken CS302 or learned what Dijkstra's algorithm is yet, then you can ignore this section -- you don't need it. If you have taken CS302, it may help you as you think about how to solve this without my hints)

The first solution that came to mind for me was a graph-based solution. Each T[i] is a node on a graph. You have an edge from each T[i] to T[j] such that (j-i) ≤ J. That edge's weight is T[j]. Then, you solve the problem by finding the shortest path through the graph, using Dijkstra's Algorithm. O(E log V).

The graph is directed and acyclic, BTW. So you can use Topological Sort, too: O(E)

The problem is that J can be as big as N, so the graph can have O(N2) edges. With N being as big as 500,000, that won't do, for both Topological Sort and Dijkstra's algorithm. I tried to think of ways to reduce the number of edges, but nothing good came to mind.


Hints - How we can use a multimap to our advantage

I generated some random T's and played with it, and the following solution popped out at me: Let's do the topcoder example, and show each iteration:

i T[i] (key,val) pairs deleted from the multimap e B[e] B[i] multimap after addding (B[i],i)
0 0 - - - 0 (0,0)
1 9 None 0 0 9 (0,0),(9,1)
2 6 None 0 0 6 (0,0),(6,2),(9,1)
3 9 None 0 0 9 (0,0),(6,2),(9,1),(9,3)
4 2 (0,0) 2 6 8 (6,2),(8,4),(9,1),(9,3)
5 9 None 2 6 15 (6,2),(8,4),(9,1),(9,3),(15,5)
6 4 (6,2) 4 8 12 (8,4),(9,1),(9,3),(12,6),(15,5)
7 1 None 4 8 9 (8,4),(9,1),(9,3),(9,7)(12,6),(15,5)
8 8 (8,4),(9,1),(9,3) 7 9 17 (9,7)(12,6),(15,5),(17,8)

When we're done, the answer is B[8], which is 17.


Running Time Analysis

We're running thorugh the each value of i, so that's N iterations. At each iteration, we're potentially deleting elements from the multimap, and then inserting an element. Each of those is O(log N). Everything else in the iteration (setting B, adding, looking at the smallest element in the multimap) is O(1). Since an element may only be deleted once, it's a maximum of N deletions. So the answer is O(N log N), which at 500,000 is less than 10,000,000. This should work within topcoder's time limits (and it does).

Another Two Interview Questions

Could you use a different data structure as an alternative to the multimap? Would it improve the running time?

To answer this, think about the operations that you're doing on the multimap.

Scroll down for the answer.











































The only operations you are doing on the multimap are: These are the operations of a priority queue, so you could use that data structure. You can implement this as a binary heap (so "heap" would be an acceptable answer rather than "priority queue"). Big-O of the operations: So from a Big-O perspective, this doesn't speed up the solution. From a practical perspective, though, it will, because heaps are more memory efficient and time efficient than balanced binary trees. Someday when I'm bored, I'll code it up -- it won't take long, since the STL has a priority queue class. I just don't have the time at present...

More examples to help you

These are the first three examples in tests.sh:
UNIX> echo 15 7 92559 49386490 | ./a.out -
17007267
UNIX> 
i T[i] (key,val) pairs deleted from the multimap e B[e] B[i] multimap after addding (B[i],i)
0 0 - - 0 (0,0)
1 20553955 0 0 20553955 (0,0)(20553955,1)
2 2035930 0 0 2035930 (0,0)(2035930,2)(20553955,1)
3 24919667 0 0 24919667 (0,0)(2035930,2)(20553955,1)(24919667,3)
4 40338582 0 0 40338582 (0,0)(2035930,2)(20553955,1)(24919667,3)(40338582,4)
5 18989631 0 0 18989631 (0,0)(2035930,2)(18989631,5)(20553955,1)(24919667,3)(40338582,4)
6 46646852 0 0 46646852 (0,0)(2035930,2)(18989631,5)(20553955,1)(24919667,3)(40338582,4)(46646852,6)
7 3530767 0 0 3530767 (0,0)(2035930,2)(3530767,7)(18989631,5)(20553955,1)(24919667,3)(40338582,4)(46646852,6)
8 1804318 (0,0) 2 2035930 3840248 (2035930,2)(3530767,7)(3840248,8)(18989631,5)(20553955,1)(24919667,3)(40338582,4)(46646852,6)
9 6478637 2 2035930 8514567 (2035930,2)(3530767,7)(3840248,8)(8514567,9)(18989631,5)(20553955,1)(24919667,3)(40338582,4)(46646852,6)
10 16229188 (2035930,2) 7 3530767 19759955 (3530767,7)(3840248,8)(8514567,9)(18989631,5)(19759955,10)(20553955,1)(24919667,3)(40338582,4)(46646852,6)
11 21007961 7 3530767 24538728 (3530767,7)(3840248,8)(8514567,9)(18989631,5)(19759955,10)(20553955,1)(24538728,11)(24919667,3)(40338582,4)(46646852,6)
12 5203458 7 3530767 8734225 (3530767,7)(3840248,8)(8514567,9)(8734225,12)(18989631,5)(19759955,10)(20553955,1)(24538728,11)(24919667,3)(40338582,4)(46646852,6)
13 37591615 7 3530767 41122382 (3530767,7)(3840248,8)(8514567,9)(8734225,12)(18989631,5)(19759955,10)(20553955,1)(24538728,11)(24919667,3)(40338582,4)(41122382,13)(46646852,6)
14 43738170 7 3530767 47268937 (3530767,7)(3840248,8)(8514567,9)(8734225,12)(18989631,5)(19759955,10)(20553955,1)(24538728,11)(24919667,3)(40338582,4)(41122382,13)(46646852,6)(47268937,14)
15 13167019 (3530767,7) 8 3840248 17007267 (3840248,8)(8514567,9)(8734225,12)(17007267,15)(18989631,5)(19759955,10)(20553955,1)(24538728,11)(24919667,3)(40338582,4)(41122382,13)(46646852,6)(47268937,14)
17007267
UNIX> echo 20 5 92559 46386490 | ./a.out -
17007267
UNIX> 
i T[i] (key,val) pairs deleted from the multimap e B[e] B[i] multimap after addding (B[i],i)
0 0 - - 0 (0,0)
1 20553955 0 0 20553955 (0,0)(20553955,1)
2 2035930 0 0 2035930 (0,0)(2035930,2)(20553955,1)
3 24919667 0 0 24919667 (0,0)(2035930,2)(20553955,1)(24919667,3)
4 40338582 0 0 40338582 (0,0)(2035930,2)(20553955,1)(24919667,3)(40338582,4)
5 18989631 0 0 18989631 (0,0)(2035930,2)(18989631,5)(20553955,1)(24919667,3)(40338582,4)
6 46646852 0 0 46646852 (0,0)(2035930,2)(18989631,5)(20553955,1)(24919667,3)(40338582,4)(46646852,6)
7 3530767 0 0 3530767 (0,0)(2035930,2)(3530767,7)(18989631,5)(20553955,1)(24919667,3)(40338582,4)(46646852,6)
8 1804318 (0,0) 2 2035930 3840248 (2035930,2)(3530767,7)(3840248,8)(18989631,5)(20553955,1)(24919667,3)(40338582,4)(46646852,6)
9 6478637 2 2035930 8514567 (2035930,2)(3530767,7)(3840248,8)(8514567,9)(18989631,5)(20553955,1)(24919667,3)(40338582,4)(46646852,6)
10 16229188 (2035930,2) 7 3530767 19759955 (3530767,7)(3840248,8)(8514567,9)(18989631,5)(19759955,10)(20553955,1)(24919667,3)(40338582,4)(46646852,6)
11 21007961 7 3530767 24538728 (3530767,7)(3840248,8)(8514567,9)(18989631,5)(19759955,10)(20553955,1)(24538728,11)(24919667,3)(40338582,4)(46646852,6)
12 5203458 7 3530767 8734225 (3530767,7)(3840248,8)(8514567,9)(8734225,12)(18989631,5)(19759955,10)(20553955,1)(24538728,11)(24919667,3)(40338582,4)(46646852,6)
13 37591615 7 3530767 41122382 (3530767,7)(3840248,8)(8514567,9)(8734225,12)(18989631,5)(19759955,10)(20553955,1)(24538728,11)(24919667,3)(40338582,4)(41122382,13)(46646852,6)
14 43738170 7 3530767 47268937 (3530767,7)(3840248,8)(8514567,9)(8734225,12)(18989631,5)(19759955,10)(20553955,1)(24538728,11)(24919667,3)(40338582,4)(41122382,13)(46646852,6)(47268937,14)
15 13167019 (3530767,7) 8 3840248 17007267 (3840248,8)(8514567,9)(8734225,12)(17007267,15)(18989631,5)(19759955,10)(20553955,1)(24538728,11)(24919667,3)(40338582,4)(41122382,13)(46646852,6)(47268937,14)
17007267
UNIX> echo 50 10 559 46386495 | ./a.out -
17007267
UNIX> 
i T[i] (key,val) pairs deleted from the multimap e B[e] B[i] multimap after addding (B[i],i)
0 0 - - 0 (0,0)
1 20553955 0 0 20553955 (0,0)(20553955,1)
2 2035930 0 0 2035930 (0,0)(2035930,2)(20553955,1)
3 24919667 0 0 24919667 (0,0)(2035930,2)(20553955,1)(24919667,3)
4 40338582 0 0 40338582 (0,0)(2035930,2)(20553955,1)(24919667,3)(40338582,4)
5 18989631 0 0 18989631 (0,0)(2035930,2)(18989631,5)(20553955,1)(24919667,3)(40338582,4)
6 46646852 0 0 46646852 (0,0)(2035930,2)(18989631,5)(20553955,1)(24919667,3)(40338582,4)(46646852,6)
7 3530767 0 0 3530767 (0,0)(2035930,2)(3530767,7)(18989631,5)(20553955,1)(24919667,3)(40338582,4)(46646852,6)
8 1804318 (0,0) 2 2035930 3840248 (2035930,2)(3530767,7)(3840248,8)(18989631,5)(20553955,1)(24919667,3)(40338582,4)(46646852,6)
9 6478637 2 2035930 8514567 (2035930,2)(3530767,7)(3840248,8)(8514567,9)(18989631,5)(20553955,1)(24919667,3)(40338582,4)(46646852,6)
10 16229188 (2035930,2) 7 3530767 19759955 (3530767,7)(3840248,8)(8514567,9)(18989631,5)(19759955,10)(20553955,1)(24919667,3)(40338582,4)(46646852,6)
11 21007961 7 3530767 24538728 (3530767,7)(3840248,8)(8514567,9)(18989631,5)(19759955,10)(20553955,1)(24538728,11)(24919667,3)(40338582,4)(46646852,6)
12 5203458 7 3530767 8734225 (3530767,7)(3840248,8)(8514567,9)(8734225,12)(18989631,5)(19759955,10)(20553955,1)(24538728,11)(24919667,3)(40338582,4)(46646852,6)
13 37591615 7 3530767 41122382 (3530767,7)(3840248,8)(8514567,9)(8734225,12)(18989631,5)(19759955,10)(20553955,1)(24538728,11)(24919667,3)(40338582,4)(41122382,13)(46646852,6)
14 43738170 7 3530767 47268937 (3530767,7)(3840248,8)(8514567,9)(8734225,12)(18989631,5)(19759955,10)(20553955,1)(24538728,11)(24919667,3)(40338582,4)(41122382,13)(46646852,6)(47268937,14)
15 13167019 (3530767,7) 8 3840248 17007267 (3840248,8)(8514567,9)(8734225,12)(17007267,15)(18989631,5)(19759955,10)(20553955,1)(24538728,11)(24919667,3)(40338582,4)(41122382,13)(46646852,6)(47268937,14)
17007267