A - 遠足とバスの配車 / Field Trip and Bus Allocation 解説 by admin 印刷ページ
gemini-3.5-flash-highSummary
This problem asks us to assign students to buses under certain conditions to minimize the number of students who feel uncomfortable because their height is strictly greater than the ceiling height of their assigned bus. While it initially looks like a complex grouping problem, simplifying the conditions reveals a simple fact: it is always optimal to assign all students to the bus with the highest ceiling.
Analysis
Simplifying the Conditions
The problem description contains the following important conditions: 1. Students in the same class must all be assigned to the same bus. 2. There is no upper limit on the number of people assigned to a single bus, and some buses may have no students assigned to them.
Finding the Optimal Assignment
For student \(j\) not to feel uncomfortable, the ceiling height \(D_i\) of their assigned bus must be greater than or equal to their height \(H_j\) (\(H_j \leq D_i\)).
Let us focus on the highest ceiling height among all buses, \(D_{\max} = \max(D_1, D_2, \ldots, D_N)\). What happens if we assign all students to this bus with the highest ceiling?
- Class condition: Since everyone rides the same single bus, the condition “students in the same class must all ride the same bus” is automatically satisfied.
- Number of uncomfortable students: The only students who will feel uncomfortable on this bus are those whose height is strictly greater than \(D_{\max}\) (\(H_j > D_{\max}\)).
If we were to assign students to any other bus (a bus with a ceiling height lower than \(D_{\max}\)), since its ceiling height is strictly less than \(D_{\max}\), the number of uncomfortable students can never decrease (and may indeed increase).
Therefore, it is always optimal to assign all students to the bus with the highest ceiling.
Conclusion
The minimum value we seek is the number of students whose height \(H_j\) is strictly greater than the maximum bus ceiling height \(D_{\max}\). We can see that the class name \(S_j\) is a “dummy condition” that has absolutely no effect on the result.
Algorithm
- Find the maximum value \(D_{\max}\) from the list of bus ceiling heights \(D\).
- Check each student’s height \(H_j\) one by one and count the number of students where \(H_j > D_{\max}\).
- Output the counted value as the answer.
Complexity
- Time Complexity: \(O(N + M)\)
- It takes \(O(N)\) to find the maximum bus ceiling height and \(O(M)\) to iterate through the students’ heights. This is fast enough for the constraints \(N \leq 10^6, M \leq 10^5\).
- Space Complexity: \(O(N + M)\)
- We need memory space to store the input data.
Implementation Details
In Python, because the input size is large (\(N \le 10^6\)), calling
input()repeatedly may cause a Time Limit Exceeded (TLE). To speed things up, we can usesys.stdin.read().split()to read all inputs at once and process them in memory.Since the input data is structured like
[D_1, ..., D_N, S_1, H_1, S_2, H_2, ...], the student heights \(H_j\) are located at every second index starting from index2+N+1. In the model solution, we can efficiently extract only the height data using slicing[2+N+1:2+N+2*M:2].Source Code
import sys
def solve():
input_data = sys.stdin.read().split()
if not input_data:
return
N = int(input_data[0])
M = int(input_data[1])
# バス天井の高さの最大値を求める
max_D = max(map(int, input_data[2:2+N]))
# 各生徒の身長のうち、max_D より大きいものの個数をカウントする
# 身長データはインデックス 2+N+1 から 2つおきに配置されている
ans = sum([1 for h in map(int, input_data[2+N+1:2+N+2*M:2]) if h > max_D])
print(ans)
if __name__ == '__main__':
solve()
This editorial was generated by gemini-3.5-flash-high.
投稿日時:
最終更新: