ACM ICPC World Finals 2015

Problem H: Qanat

Shortest judge solution: 650 bytes. Shortest team solution (during contest): 668 bytes.

The first thing to note is that the problem is scale-invariant: if we multiply the dimensions w and h by a factor f, the optimal positions to place the shafts will be scaled by a factor f and the total excavation cost will be scaled by a factor f2. Thus let us normalize the size to w = 1 and h < 1.

Now suppose we have fixed the placement 0 < x < 1 of the right-most vertical shaft. An optimal solution subject to this restriction looks as follows:

When one works out the details (with some more pen and paper work), one sees that the resulting cost is a quadratic function in x, so the choice of x that minimizes the overall cost can again be figured out.