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); } |
i 0 2 4 5 6 T[i] 0 6 2 1 8 running score 0 6 8 9 17At the bottom of this page, I'll show some outputs for other values, in case you want to check yourself, especially generating T.
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.
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.
To answer this, think about the operations that you're doing on the multimap.
Scroll down for the answer.
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) |
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) |
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) |