Editorial for COCI '10 Contest 1 #1 Timsko


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.

Observe that a team can be formed if three conditions are satisfied: the number of girls is at least 2, the number of boys is at least 1, and M+N \ge K+3 holds (since a team consists of three students, and K students need to go on an internship). We naturally arrive at a greedy algorithm - forming teams as long as the conditions are met. More precisely, the pseudocode is as follows:

while (M ≥ 2 and N ≥ 1 and M+N ≥ K+3) do
{
    result := result+1;     (increment the number of formed teams)
    M := M-2;               (decrease the number of girls)
    N := N-1;               (decrease the number of boys)
}

Alternative solution

If there are at least twice as many girls as there are boys, we can say that they form a surplus regarding team formation, otherwise the boys form a surplus. Thus, we can repeat K times: check if there is a surplus of girls; if so, decrement the number of girls (i.e. invite a girl to the internship), otherwise decrement the number of boys (i.e. invite a boy to the internship). In the end, we calculate the number of teams we can form from the remaining boys and girls.


Comments

There are no comments at the moment.