Editorial for COCI '09 Contest 1 #4 Mali


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.

Let's try to solve the following subproblem: Given two sequences A and B, what is the optimal pairing for these two sequences? Let Amax be the largest number in sequence A (if more than one exists, choose any one), and Bmin be the smallest number in sequence B. Choose some pairing which contains the pair (Amax,Bmin). Is this optimal? There are two cases we need to address:

  • (Amax+Bmin) is the largest sum in the chosen pairing. If this is the case, it is quite obvious that no other number from B can reduce this sum. The best you can do is select another instance of Bmin, if more than one is present, and achieve the exact same sum. So, if (Amax+Bmin) is the largest sum in the chosen pairing, there is no way to improve that.

  • (Amax+Bmin) is not the largest sum in the chosen pairing. Let (Ax,Bx) denote the pair with the largest sum. Can we improve this by breaking the (Amax,Bmin) pair? We could try creating pairs (Amax,Bx) and (Ax,Bmin). It is clear that now the sum Ax+Bmin is equal or smaller than Ax+Bx. However, Amax+Bx is now larger or equal than Ax+Bx because Amax is larger than or equal to Ax. This has now created a new largest sum, larger than or equal to the previous one.

So we conclude that it is not possible to improve the solution by not using (Amax,Bmin). This gives us a good starting point for a greedy approach.


Comments

There are no comments at the moment.