This page is still under construction.

Parts of this page are still being built. What you see may change.

Shark Tour

Time limit1sMemory limit256 MB

Summary
Steer a car through a grid tunnel with blocked cells to collect the most sharks and print the tie-breaking action sequence.
Level

Medium5 of 10

Topics
Dynamic programming, Matrix
Solved
No attempts yet

Problem

The minions who rode in Lucy's car on an underwater trip were impressed by the many sharks they saw in the tunnel. When they got home they told the other minions, and now everyone wants to go back and see the sharks.

The sonar on Lucy's car reports the depth and the length of the tunnel together with the height and the distance of every stalagmite that rises from the tunnel floor. For the shark tour Lucy modified the sonar so that it also reports how many sharks can be seen from each location in the tunnel. Because of the viewing conditions, a shark is visible from exactly one location.

A current runs through the tunnel and carries the car 1 meter forward each second. Every second Lucy can steer the car up 1 meter, keep it at the same depth, or steer it down 1 meter.

Treat the tunnel as a grid. A tunnel of depth DD and length NN contains one location (d,p)(d, p) for every distance dd with 0≤d≤N−10 \le d \le N-1 and every depth pp with 0≤p≤D−10 \le p \le D-1. Depth 00 is the ceiling and depth D−1D-1 is the floor. A stalagmite of height hh standing at distance dd fills the hh lowest locations of that column, which are the depths D−hD-h through D−1D-1. The car can never enter a filled location and can never leave the tunnel.

The car starts at the top left corner, distance 00 and depth 00, and it has safely reached the end of the tunnel once it has traveled N−1N-1 meters. A path is therefore a sequence of N−1N-1 actions, each written as one character. ^ steers the car up and decreases the depth by 1, > keeps it at the same depth, and v steers it down and increases the depth by 1. The car sees the sharks of every location it occupies, including the starting location and the last one. At least one path through the tunnel always exists.

The tunnel in the figure has depth 3 and length 5. One stalagmite is 1 meter high at distance 2, the other is 2 meters high at distance 3. The sonar reports 4 sharks at distance 2 depth 1, 3 sharks at distance 2 depth 0, 2 sharks at distance 4 depth 1, and 6 sharks at distance 1 depth 2. One second after the start the car is at depth 0 or depth 1, so it can never reach distance 1 depth 2. The action sequence >v^v passes through distance 2 depth 1 and distance 4 depth 1 and sees 6 sharks, and no path sees more.

Find the largest number of sharks a legal path sees, and the actions of such a path. The tunnel can be long and the sonar can report several thousand sightings, so checking every path one by one is too slow.

Input

The first line contains the number of test cases TT. Each test case is written in the form below. The spacing between the words and the indentation of the lines varies, so reading the integers in order is enough.

Tunnel depth D length N
	S stalagmites
	h meter stalagmite d meters distant
	K sightings
	c sharks d meters distant p meters down

The stalagmite line is repeated SS times and the sighting line is repeated KK times.

1≤T≤201 \le T \le 20, 1≤D≤1001 \le D \le 100, 2≤N≤100002 \le N \le 10000, 0≤S≤N0 \le S \le N, 1≤h≤D−11 \le h \le D-1, 0≤K≤50000 \le K \le 5000, 1≤c≤10001 \le c \le 1000, and every distance satisfies 0≤d≤N−10 \le d \le N-1 while every sighting depth satisfies 0≤p≤D−10 \le p \le D-1. The sum of D×ND \times N over all test cases is at most 200000200000. Every test case admits at least one path from the start to distance N−1N-1. Two sighting lines can name the same location, and then their shark counts add up.

Output

For each test case print two lines. The first line is Case: t, where tt is the number of the test case counted from 1. The second line is

Saw n sharks for sequence s

where nn is the largest number of sharks a legal path sees and ss is the N−1N-1 actions of such a path.

Several paths can see nn sharks. Compare two action sequences character by character and look at the first position where they differ: the sequence whose character comes earlier in the order >, ^, v comes first. Print the sequence that comes first under this rule.

Examples3

  1. Example 1

    Input
    2
    Tunnel depth 3 length 5 
    	0 stalagmites
    	1 sightings
    		10 sharks 2 meters distant 1 meters down
    		
    Tunnel depth 3 length 5 
    	2 stalagmites
    		1 meter stalagmite 2 meters distant
    		2 meter stalagmite 3 meters distant
    	4 sightings
    		4 sharks 2 meters distant 1 meters down
    		3 sharks 2 meters distant 0 meters down
    		2 sharks 4 meters distant 1 meters down
    		6 sharks 1 meters distant 2 meters down
    
    Expected output
    Case: 1
    Saw 10 sharks for sequence >v>>
    Case: 2
    Saw 6 sharks for sequence >v^v
    
  2. Example 2

    Input
    1
    Tunnel depth 1 length 6
    	0 stalagmites
    	2 sightings
    		5 sharks 0 meters distant 0 meters down
    		7 sharks 3 meters distant 0 meters down
    
    Expected output
    Case: 1
    Saw 12 sharks for sequence >>>>>
    
  3. Example 3

    Input
    1
    Tunnel depth 3 length 4
    	0 stalagmites
    	3 sightings
    		4 sharks 1 meters distant 1 meters down
    		5 sharks 2 meters distant 0 meters down
    		5 sharks 2 meters distant 2 meters down
    
    Expected output
    Case: 1
    Saw 9 sharks for sequence v^>