Skip to content

7 min read中文

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.

hero.webp

A

Amalgamated Artichokes

Problem

Define price(k)=p(sin(ak+b)+cos(ck+d)+2)\operatorname{price}(k)=p\cdot(\sin(a\cdot k+b)+\cos(c\cdot k+d)+2), where p,a,b,c,dp,a,b,c,d are given parameters. Given the sequence S=[price(1),price(2),...,price(n)]S=[\operatorname{price}(1),\operatorname{price}(2),...,\operatorname{price}(n)], find max1ijnSiSj\max\limits_{1\le i\le j\le n} {S_i-S_j}.

Solution

Only a prefix maximum can serve as the answer’s SiS_i; the answer’s SjS_j is then the minimum between SiS_i and the next prefix maximum. A single left-to-right scan suffices.

Code

B

Asteroids

C

Catering

Problem

An equipment rental company receives nn requests listed in chronological order.

The company has kk porters. Each porter can carry one set of equipment, in chronological order, to fulfill some requests. Moving equipment from the location of request ii to the location of request jj costs a porter some amount. The company is numbered 11, and the requests are numbered 2n+12\sim n+1. Every porter must start from the company.

Find the minimum total cost to fulfill all requests.

Solution

  • Connect S1S\rightarrow 1 with an edge of capacity [0,k][0,k] and cost 00
  • Split 2n+12\sim n+1 into two vertices ui,viu_i,v_i each, connected by an edge of capacity [1,1][1,1] and cost 00
  • Connect viTv_i\rightarrow T with an edge of capacity [0,1][0,1] and cost 00
  • Connect 1ui1\rightarrow u_i with an edge of capacity [0,1][0,1] and cost dis1,idis_{1,i}
  • Connect viuj(i<j)v_i\rightarrow u_j(i\lt j) with an edge of capacity [0,1][0,1] and cost disi,jdis_{i,j}

Then use a feasible flow with lower and upper bounds on the arcs with a designated source.

Code

D

Cutting Cheese

Problem

A square block of cheese has nn spherical holes in it, and the holes do not overlap. You want to cut the cheese vertically into ss slices of equal weight; find the thickness of each slice.

Solution

The volume of a sphere is 43πR3\frac 4 3\pi\cdot R^3. The volume of a spherical cap is πh2(Rh3)\pi\cdot h^2\cdot(R-\frac h 3) or 16πh(3r2+h2)\frac 1 6\pi\cdot h\cdot(3r^2+h^2), where RR is the radius of the sphere, hh is the height of the cap, and rr 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.

Code

E

Evolution in Parallel

Problem

You are given nn strings over the alphabet {A,C,M}\{A,C,M\}. You need to split them into 22 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 ss.

Solution

If any of the nn strings is not a subsequence of ss, the answer is clearly impossible.

Otherwise, first sort the strings by length. Maintain two columns f,gf,g. Now consider adding a string strstr, split into three cases.

  • If it can be added to neither ff nor gg, the answer is impossible
  • If it can be added to both ff and gg, put it into a standby sequence tt. Every string in tt must be appendable both to the current ff and to the current gg. If tt is non-empty and strstr cannot be appended to tt, append strstr and tt to f,gf,g
  • If it can only be added to one of ff or gg, append strstr to that column, and append tt to the other column

Code

F

Keyboarding

Problem

Given a virtual keyboard with rr rows and cc columns, you can move an on-screen cursor to print text using 55 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 visx,yvis_{x,y} denote the furthest position in the text string that can be matched when passing through position (x,y)(x,y); during a transition, if the matched position does not increase, that transition can be skipped (this is clearly correct).

Code

G

Pipe Stream

H

Qanat

Problem

A qanat is an irrigation system consisting of an underground water source and several vertical wells.

2016_final_H.png

In this problem, it is abstracted as the model shown above.

The wells ABA\rightarrow B and BCB\rightarrow C must be dug. You must also dig nn vertical wells in between. A vertical well’s horizontal coordinate can be any real number in [0,w][0,w]. The excavated dirt must be transported to ACAC (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 nn vertical wells so as to minimize the total cost.

I

Ship Traffic

Problem

A river is divided into nn east-west lanes (1n1051\le n\le 10^5), each lane ww wide.

A point departs from somewhere on the riverbank (hereafter called the origin) at some time within [t1,t2][t_1,t_2], crossing this swimming pool at constant speed vv from south to north. Lane ii (i{0,1,2,...,n1}i\in\{0,1,2,...,n-1\}) contains mim_i boats moving at constant speed uu in the east / west direction; initially, the bow of each boat is at distance pi,jp_{i,j} from the north-south line through the origin, and the boat’s length is li,jl_{i,j}. From when the point enters lane ii 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 [li,ri)[l_i,r_i).

Specifically, for a boat of length ll in lane ii, the bow reaches this line at time pu\frac p u, and the stern leaves this line at time p+lu\frac {p+l} u; the point cannot be within the lane during pup+lu\frac p u\sim\frac {p+l} u. 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 puw(i+1)vp+luwiv\frac p u-\frac{w\cdot(i+1)}v\sim\frac{p+l}u-\frac{w\cdot i}v.

So a straightforward difference array suffices.

Code

J

Tile Cutting

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 nn queries, among parallelograms with area between ala_l and ara_r, find which area value has the most parallelograms.

Solution

A parallelogram inscribed in the sheet, with the four corner triangles shaded

The area of the parallelogram equals the area of the enclosing rectangle minus the areas of the four small triangles.

(a+d)(b+c)abcd=ac+bd(a+d)\cdot(b+c)-a\cdot b-c\cdot d=a\cdot c+b\cdot d

Let d(x)d(x) denote the number of divisors of xx, and ans(x)ans(x) denote the number of parallelograms with area xx. Then ans(x)=i+j=xd(i)d(j)ans(x)=\sum\limits_{i+j=x}d(i)\cdot d(j). This is clearly a convolution, so apply FFT directly.

Code

K

Tours

Problem

Given a simple graph GG containing at least one cycle, find all integers kk such that the edges of GG can be coloured with kk colours so that every simple cycle contains the same number of edges of each of the kk colours.

Solution

A structural result. Ignore the bridges in the graph (these clearly cannot affect the answer); for each remaining edge ii, compute the number of new bridges wiw_i created by removing ii. The answer is the gcd\gcd of all wi+1w_i+1. See this proof backup.

Code

L

Weather Report

Problem

Consider 44 kinds of weather, sunny / cloudy / rainy / foggy, with known probabilities psunny,pcloudy,prainy,pfrogp_\text{sunny},p_\text{cloudy},p_\text{rainy},p_\text{frog} respectively. You need to transmit the weather for the next nn days. You want to binary-encode these 4n4^n 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.

Code

M

Window Manager