Editorial: 2015 ACM-ICPC World Finals - Marrakech
Editorial for a virtual run of the 2015 ACM-ICPC World Finals in Marrakech, covering min-cost flow, binary search, subsequence matching, pruned BFS, and Huffman coding.
Machine-translated from the Chinese original.

A
Problem
Define , where are given parameters. Given the sequence , find .
Solution
Only a prefix maximum can serve as the answer’s ; the answer’s is then the minimum between and the next prefix maximum. A single left-to-right scan suffices.
B
C
Problem
An equipment rental company receives requests listed in chronological order.
The company has porters. Each porter can carry one set of equipment, in chronological order, to fulfill some requests. Moving equipment from the location of request to the location of request costs a porter some amount. The company is numbered , and the requests are numbered . Every porter must start from the company.
Find the minimum total cost to fulfill all requests.
Solution
- Connect with an edge of capacity and cost
- Split into two vertices each, connected by an edge of capacity and cost
- Connect with an edge of capacity and cost
- Connect with an edge of capacity and cost
- Connect with an edge of capacity and cost
Then use a feasible flow with lower and upper bounds on the arcs with a designated source.
D
Problem
A square block of cheese has spherical holes in it, and the holes do not overlap. You want to cut the cheese vertically into slices of equal weight; find the thickness of each slice.
Solution
The volume of a sphere is . The volume of a spherical cap is or , where is the radius of the sphere, is the height of the cap, and is the radius of the cap’s base.
Binary search directly on the current slice’s thickness, computing the volume of each hole with the two formulas above.
E
Problem
You are given strings over the alphabet . You need to split them into columns such that within each column, each string is a subsequence of the next, and the last string in each column is a subsequence of a given string .
Solution
If any of the strings is not a subsequence of , the answer is clearly impossible.
Otherwise, first sort the strings by length. Maintain two columns . Now consider adding a string , split into three cases.
- If it can be added to neither nor , the answer is
impossible - If it can be added to both and , put it into a standby sequence . Every string in must be appendable both to the current and to the current . If is non-empty and cannot be appended to , append and to
- If it can only be added to one of or , append to that column, and append to the other column
F
Problem
Given a virtual keyboard with rows and columns, you can move an on-screen cursor to print text using control keys: up / down / left / right / select.
Initially, the cursor is at the top-left of the keyboard. Each press of an arrow key always jumps the cursor to the next character in that direction that differs from the current character; if no such character exists, the cursor does not move. Each press of the select key prints the character at the cursor’s current position.
Find the minimum number of key presses needed to print a given text (a newline must be printed at the end).
Solution
This one’s just brute-force search plus pruning, that’s it.
First precompute, for each position, where jumping one step up / down / left / right leads, then just brute-force a BFS over it. For pruning, let denote the furthest position in the text string that can be matched when passing through position ; during a transition, if the matched position does not increase, that transition can be skipped (this is clearly correct).
G
H
Problem
A qanat is an irrigation system consisting of an underground water source and several vertical wells.

In this problem, it is abstracted as the model shown above.
The wells and must be dug. You must also dig vertical wells in between. A vertical well’s horizontal coordinate can be any real number in . The excavated dirt must be transported to (ground level); dirt at any position can be transported arbitrarily in the horizontal and vertical directions, and the cost is the shortest distance. You are asked to arrange the positions of these vertical wells so as to minimize the total cost.
I
Problem
A river is divided into east-west lanes (), each lane wide.
A point departs from somewhere on the riverbank (hereafter called the origin) at some time within , crossing this swimming pool at constant speed from south to north. Lane () contains boats moving at constant speed in the east / west direction; initially, the bow of each boat is at distance from the north-south line through the origin, and the boat’s length is . From when the point enters lane until it leaves, no boat may cross this north-south dividing line.
Find the length of the longest feasible departure time interval.
Solution
Each boat’s information can be converted into a constraint cannot depart during this time interval .
Specifically, for a boat of length in lane , the bow reaches this line at time , and the stern leaves this line at time ; the point cannot be within the lane during . Consider the two limiting valid cases: the point exits the region exactly when the bow reaches the line, and the point enters the region exactly when the stern leaves the line. So the point cannot depart during .
So a straightforward difference array suffices.
J
Problem
On a rectangular grid sheet of a given length and width, pick four points on the four edges (not including the corners) and connect them in order to form a parallelogram. For queries, among parallelograms with area between and , find which area value has the most parallelograms.
Solution

The area of the parallelogram equals the area of the enclosing rectangle minus the areas of the four small triangles.
Let denote the number of divisors of , and denote the number of parallelograms with area . Then . This is clearly a convolution, so apply FFT directly.
K
Problem
Given a simple graph containing at least one cycle, find all integers such that the edges of can be coloured with colours so that every simple cycle contains the same number of edges of each of the colours.
Solution
A structural result. Ignore the bridges in the graph (these clearly cannot affect the answer); for each remaining edge , compute the number of new bridges created by removing . The answer is the of all . See this proof backup.
L
Problem
Consider kinds of weather, sunny / cloudy / rainy / foggy, with known probabilities respectively. You need to transmit the weather for the next days. You want to binary-encode these possible weather sequences such that no sequence’s code is a prefix of another’s. Find the minimum expected code length.
Solution
Huffman coding. Group together the cases with equal occurrence probability when computing.