Editorial for ICPC PACNW 2016 I - Postman
Submitting an official solution before solving the problem yourself is a bannable offence.
Postman is a simulation problem, but one with a few traps.
The basic approach is to simply deliver as many letters as possible to the furthest houses first; you must make at least that many trips at that distance, so this is optimal. If you instead try to deliver to the closest houses first, you may end up with a suboptimal solution; consider the case:
House | Distance | Letters |
---|---|---|
with a postman capacity of . If we do the close house first, then the postman will deliver letters to house , and then to house —but will need to make a second trip to house , for a total time of round trips at distance which is time units.
If instead he takes a round trip to house first, he'll deliver all letters to house in one round trip and finish up with one final trip to house carrying only letters for a total of .
The problem is the number of letters and the distances can be large (although the count of houses is small). If you simulate each individual trip, you will run out of time, because the total number of letters could be as high as . So you want to figure out how many round trips to a given house are required and, with a tiny bit of math, figure out how many letters will be left over to drop off along the route of the last round trip to the next closer house (and possibly additional houses).
The second trap is a common one—the result may not fit in a 32-bit int so you need to use doubles or 64-bit ints.
Comments