Editorial for COCI '10 Contest 1 #1 Timsko
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 , the number of boys is at least , and holds (since a team consists of three students, and 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 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