Vendors sharing a corner split one step east and one step west per move, and the task asks for the fewest moves that leave every vendor on a distinct corner.
Medium7Dynamic programmingSortingNo attempts yetTime limit5sMemory limit512 MBHot dog vendors have set up at the corners (intersections) of a very long east-west street. The trouble is that several vendors can end up at the same corner, and then they take business from each other. There is still a way out. The vendors have a plan.
Whenever a corner holds two or more vendors, exactly two of them can perform a move, which means:
The street is really long, so the vendors never run out of corners. Given the starting positions of all hot dog vendors, find the minimum number of moves they need to perform before the vendors are all separated, meaning they are all on different corners.
For example, suppose the number of hot dog vendors on each corner, listed in order from west to east, starts like this.
... 0 0 2 1 2 0 0 ...
Then the vendors can be separated in three moves, as shown below.
... 0 0 2 1 2 0 0 ...
|
+--- Do a move here
... 0 1 0 2 2 0 0 ...
|
+--- Do a move here
... 0 1 1 0 3 0 0 ...
|
+--- Do a move here
... 0 1 1 1 1 1 0 ...
Each street corner is labeled with an integer, positive or negative. For each i, corner i+1 is the next corner to the east from corner i. The input uses this labeling to describe corners.
The first line contains the number of cases T. T test cases follow. Each case begins with the number of corners C that have at least one hot dog vendor in the starting configuration. The next C lines each contain a pair of space-separated integers P and V, indicating that there are V vendors at corner P.
For each test case, output one line containing Case #x: M, where x is the case number starting from 1 and M is the minimum number of moves that need to be performed before the vendors all end up at different corners from each other.