Problem:
UVa 11389 - The Bus Driver Problem
Summary:
Given a function
f(x)=xif x ≥00if x <0
and two ordered sets ai and bi, find the minimum S = r∗∑f(a+b−d) for some r,d≥1.
There is a large set of possibilities for pairing a and b, but there is an intuitive (or maybe not) arrangement for a and b that can hopefully minimize S, that is by first sorting ai and bi, and pairing the largest a with the smallest b sequentially. The code is very short, and it is indeed the solution. Now it remains to prove that our algorithm is really correct.
To do so, we consider the case when we only have {a1,a2} and {b1,b2}, with some value d. For the ease of discussion, suppose that a1≤a2 and b1≤b2 (if not, we can swap the values to obtain this configuration). We need to prove that S=f(a1+b1−d)+f(a2+b2−d) is the smallest S possible, that is,
Lemma 1:
For any a, b, and d, we have S≤T where T=f(a1+b2−d)+f(a2+b1−d).
Proof:
Firstly observe that a1+b1≥a1+b2 and a2+b2≤a2+b1.
Consider a few cases:
1. If a1+b1−d>0 and a2+b2−d>0, then
S=(a1+b1−d)+(a2+b2−d)
S=(a1+b2−d)+(a2+b1−d)
S≤f(a1+b2−d)+f(a2+b1−d)=T
2.If a1+b1−d<0 and a2+b2−d>0 or a1+b1−d<0 and a2+b2−d<0, the proposition is trivially true.
3. Lastly, if a1+b1−d>0 and a2+b2−d<0, notice that a1+b1>a2+b2. We show that a2+b1 cannot be less than d (since otherwise, S will be more than T). Suppose a2+b1<d, then d≤a1+b1≤a2+b1<d, a contradiction. As such the proof is complete.
Now, using the above lemma, we can prove that our arrangement is indeed the smallest. Consider a pairing {(a1,b′1),(a2,b′2),…,(an,b′n)} for some arrangement of b. We can find a pair of (ai,bi) and (aj,bj) such that bi<bj (since otherwise b is already sorted in reverse order). Applying Lemma 1, we will obtain a new arrangement with a decreasing total sum, until we cannot find anymore such pairs, in which we obtain the desired arrangement of a and b! :D
I don't understand, How : a_1 + b_1 >= a_1 + b_2 ? Since b_1 <= b_2, therefore it should be a_1 + b_1 <= a_1 + b_2
ReplyDelete