Editorial for IOI '11 P3 - Rice Hub


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

The key insight to solving this problem is the observation that for any K rice fields located at r0r1rK1, the transportation cost from all these K fields is minimized by placing the rice hub at a median. For example, when K=1, the hub should be at r0, and when K=2, placing it between r0 and r1 is optimal. In this problem, we will place the rice hub at rK/2 for simplicity. Following this observation, we denote a solution by a sequence Sr0,,rR1 and let |S| denote the length of S, which is the solution's value (the number of rice fields whose rice will be transported to the hub). The cost of S is cost(S)=rjS|rjh(S)|, where h(S) is the |S|2-th element of S.

An O(R3) solution

Armed with this, we can solve the task by a guess-and-verify algorithm. We try all possible lengths of S (ranging between 1 and R). Next observe that in any optimal solution S, the rice fields involved must be contiguous; that is, S is necessarily rs,rs+1,,rt for some 0stR1. Therefore, there are RK+1 solutions of length K. For each choice of S, we compute h(S) and the transportation cost in O(|S|) time and check if it is within the budget B. This leads to an O(R3) algorithm, which suffices to solve subtask 2.

An O(R2) solution

To improve it to O(R2), we will speed up the computation of cost(S). Notice that we are only dealing with consecutive rice fields. Thus, for each S, the cost cost(S) can be computed in O(1) after precomputing certain prefix sums. Specifically, let T[i] be the sum of all coordinates to the left of rice field i, i.e., T[0]=0 and T[i]=j=0i1X[j]. Then, if S=rs,,rt, cost(S) is given by (ps)rp(T[p]T[s])+(T[t+1]T[p+1])(tp)rp, where p=s+t2. This O(R2) algorithm suffices to solve subtask 3.

An O(RlogR) solution

Applying a binary search to find the right length in place of a linear search improves the running time to O(RlogR) and suffices to solve all subtasks.

An O(R) solution

We replace binary search with a variant of linear search carefully designed to take advantage of the feedback obtained each time we examine a combination of rice fields. In particular, imagine adding in the rice fields one by one. In iteration i, we add ri and find (1) Si, the best solution that uses only (a subsequence of) the first i rice fields (i.e., Sir0,,ri1), and (2) Si, the best solution that uses only (a subsequence of) the first i rice fields and contains ri1. This can be computed inductively as follows. As a base case, when i=0, both Si and Si are just r0 and cost 0, which is within the budget B0. For the inductive case, assume that Si and Si are known. Now consider that Si+1 is Si appended with ri, denoted by Siri, if the cost cost(Siri) is at most B, or otherwise it is the longest prefix of Siri that costs at most B. Furthermore, Si+1 is the better of Si and Si+1. To implement this, we represent each Si by its starting point s and ending point t; thus, each iteration involves incrementing t and possibly s, but s is always at most t. Since cost(rs,,rt) takes O(1) to compute, the running time of this algorithm is O(R) and suffices to solve all subtasks.


Comments

There are no comments at the moment.