'Looking for a faster solution to 'maximum number of teams' problem
My solution exceeds the time limit and I can't come up with a faster solution, still very much a beginner. How can I improve it?
The problem:
A perfect ICPC team is made up of at least 1 mathematician and 1 programmer and it must have 3 members. You have the number of mathematicians, programmers and students that have no specialization. What is the maximum number of perfect teams you can make? C students are programmers, M students are mathematicians and X don't have a specialization.
Example input:
1 1 1
Example output:
1
Example input:
3 6 0
Example output:
3
Example input:
10 1 10
Example output:
1
My solution:
cmx = [int(x) for x in input().split()]
i = 0
while 0 not in cmx:
cmx[0] -= 1
cmx[1] -= 1
cmx[2] -= 1
i += 1
if cmx[0] != 0 and cmx[1] != 0 and cmx[2] == 0:
while sum(cmx) >= 3 and cmx[0] != 0 and cmx[1] != 0:
if cmx[0] >= cmx[1]:
cmx[0] -= 2
cmx[1] -= 1
i += 1
elif cmx[0] < cmx[1]:
cmx[0] -= 1
cmx[1] -= 2
i += 1
print(i)
Solution 1:[1]
Assume that M ? C. (Proof works identically if C ? M). How many teams can I make. It's clear that if M + C + X ? 3M, then I can easily make M teams. (Every team has a mathematician, a programmer, and either a second programmer or a "none".) And I can't make more than M teams. If M + C + X < 3M, then the most I can have is (M + C + X) / 3 teams, and again you make them the same way, since you have sufficient mathematicians and programmers.
The proof works identically if C ? M.
So min(M, C, (M + C + X) // 3). As stated above.
A simpler way of looking at it is that C, M, and (C + M + X)//3 are each, independently, an upper bound on the number of teams that you can form. You just have to show the smallest of these three upper bounds is, in fact, a reachable value.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
