Supercap Travels
Time limit1sMemory limit128 MB
For each city distance, compute the best trip fare under the doubling speed profile and output the highest-scoring city per region.
- Level
Medium6 of 10
- Topics
- Math, Greedy, Implementation
- Solved
- No attempts yet
Problem
In 2037, two and a quarter centuries after the first commercial railway locomotive, supersonic magnetic levitation capsules called supercaps entered service. A supercap runs at up to m/s, so a trip between two cities takes seconds instead of hours.
A supercap changes speed instantly, and every speed it holds is a power of two in m/s. One trip covers exactly meters, starts at rest, ends at rest, and has four parts.
- Acceleration. The capsule runs one second at m/s, one second at m/s, one second at m/s, and so on, doubling every second until it reaches its upper speed . Every speed on the way, included, is held for exactly one second.
- Upper glide. The capsule holds for further seconds, where .
- Deceleration. The capsule halves its speed every second, from down to the fixed lower speed of m/s. Every speed on the way, m/s included, is held for exactly one second.
- Final glide. The capsule holds m/s for a whole number of seconds, then m/s, then m/s, then m/s, then m/s, and then it stops. Each of these five counts may be zero, so a speed may be skipped.
The speed against time graph below shows a typical trip.

The upper speed is not a free choice. The capsule accelerates as far as it can, so is the largest power of two, at most , whose acceleration and deceleration parts alone already fit inside the trip: .
The fare of a trip is proportional to
where is the number of seconds spent above m/s and is the number of seconds spent at m/s or below. Time above the lower speed earns money, and one second at or below the lower speed costs the corporation 100 times what one second above it earns. Every city is priced by its best trip, so the fare score of a city at distance is the largest value of over all trips that cover exactly meters.
In the first build phase the capital city of a region gets one link into each neighbouring region, and that link reaches exactly one city of that region. Cities inside the same region are never linked to each other. For every neighbouring region, report the city with the largest fare score.
Input
The first line contains an integer (), the number of test cases.
Each test case begins with a line holding an integer (), the number of neighbouring regions. The descriptions of the regions follow in order.
Each region description begins with a line holding an integer (), the number of cities of that region that have a supercap station. Each of the next lines holds a city name and an integer (). The name is one word of English letters, and is the distance in meters from the station in the capital city to the station in city . Inside one region all distances are distinct.
Output
Print one line for each test case. The line starts with Case #x:, where is the number of the test case counting from , then one space, then the names of the chosen cities separated by single spaces. The names come in the same order as the regions of that test case. If several cities of one region reach the same largest fare score, choose the one that appears first in the input.