#T566. 组队问题
组队问题
Description
OJ has introduced many new features in version 3.0, including the team functionality. However, as this is a new feature and the developers' skills are not yet mature, there may be n bugs during usage. Therefore, once a bug is discovered, administrators recommend that COIers report it to the feedback email (question@codesonline.cn).
Due to the high volume of feedback and the inability of OJ team members to properly divide tasks, administrators suggest forming teams to handle the feedback issues.
There are two ways to form teams:
Type 1: 3 members + 1 email receiver
Type 2: 10 members + 4 email receivers
Input Format
The input consists of a single line with two numbers, n and m, representing the number of members and the number of email receivers, respectively.
Output Format
The output consists of two lines:
- The first line: Output the minimum number of teams formed (while minimizing the remaining number of unassigned people).
- The second line: Output the remaining number of people (if none remain, output
0).
If it is impossible to form any teams, output 0.
Example Input 1
13 5
Example Output 1
1
5
Example Input 2
10 4
Example Output 2
1
0
Example Input 3
5 1
Example Output 3
0
Constraints
Solution
To solve this problem, we need to determine the optimal number of teams of Type 1 and Type 2 such that the total number of teams is minimized while ensuring that the remaining members and email receivers are as few as possible. If no valid configuration exists, we should return 0.
Approach
- Iterate Over Possible Team Combinations: For each possible number of Type 2 teams (from 0 to the maximum possible), compute the remaining members and email receivers. Then, check if the remaining resources can form Type 1 teams.
- Check Feasibility: For each combination of Type 2 and Type 1 teams, verify if the total members and email receivers used match or are less than the given
nandm. - Track Optimal Solution: Keep track of the combination that uses the most resources (i.e., leaves the fewest remaining people) and has the smallest number of total teams.
Solution Code
n, m = map(int, input().split())
min_teams = float('inf')
remaining_people = float('inf')
found = False
max_type2 = min(n // 10, m // 4)
for type2 in range(max_type2, max(max_type2 - 100, -1), -1):
remaining_n = n - 10 * type2
remaining_m = m - 4 * type2
if remaining_n < 0 or remaining_m < 0:
continue
type1 = min(remaining_n // 3, remaining_m // 1)
total_teams = type2 + type1
remaining_people_current = (remaining_n - 3 * type1) + (remaining_m - 1 * type1)
if type1 >= 0 and (remaining_n >= 3 * type1) and (remaining_m >= 1 * type1):
if total_teams > 0:
if total_teams < min_teams or (total_teams == min_teams and remaining_people_current < remaining_people):
min_teams = total_teams
remaining_people = remaining_people_current
found = True
if not found:
max_type1 = min(n // 3, m // 1)
for type1 in range(max_type1, max(max_type1 - 100, -1), -1):
remaining_n = n - 3 * type1
remaining_m = m - 1 * type1
if remaining_n < 0 or remaining_m < 0:
continue
type2 = min(remaining_n // 10, remaining_m // 4)
total_teams = type1 + type2
remaining_people_current = (remaining_n - 10 * type2) + (remaining_m - 4 * type2)
if type2 >= 0 and (remaining_n >= 10 * type2) and (remaining_m >= 4 * type2):
if total_teams > 0:
if total_teams < min_teams or (total_teams == min_teams and remaining_people_current < remaining_people):
min_teams = total_teams
remaining_people = remaining_people_current
found = True
if found:
print(min_teams)
print(remaining_people if remaining_people > 0 else 0)
else:
print(0)
Explanation
- Initialization: The variables
min_teamsandremaining_peopleare initialized to track the optimal solution. - Type 2 Team Check: The loop iterates over possible numbers of Type 2 teams (up to the maximum feasible based on
nandm). For each, it checks if the remaining resources can form Type 1 teams. - Feasibility Check: For each combination, the code verifies if the remaining members and email receivers can form valid teams, updating the optimal solution if a better configuration is found.
- Type 1 Team Check: If no valid Type 2 combinations are found, the code checks combinations starting with Type 1 teams.
- Output Result: If a valid configuration is found, it outputs the minimal teams and remaining people; otherwise, it outputs
0.
This approach efficiently explores possible team configurations to find the optimal solution while handling edge cases where no valid teams can be formed.
3 1
1
0
13 6
2
1
Hint
Data range:
Source
Original problem from CodesOnline