B - 身長比べ / Height Comparison Editorial by admin
Qwen3-Coder-480BOverview
Given students lined up in order of their student numbers, count the number of students who “have a shorter height than someone in the tall group who is ahead of them.”
Analysis
In this problem, for each student \(i\), we need to determine whether there is at least one person among the students before them who belongs to the “tall group (height \(\geq T\))” and is “taller than them.”
Naive Approach and Its Problem
A naive method would be, for each student, to check all students before them to see if anyone satisfies the condition. However, this requires \(O(N^2)\) computation in the worst case, which is impractical since \(N\) can be up to \(10^6\) (TLE).
Furthermore, even if we brute-force search through “all tall group members before the current student,” we still need to compare height relationships, making the information insufficient.
Improvement: Only Track the “Maximum Height of the Tall Group”
The key observation is that we only need to remember the height of the tallest person among the tall group students before the current student. This is because if there exists a “tall group member who is taller than the current student” before them, then the “tallest person in the tall group” will necessarily be taller as well.
Therefore, as we scan students from left to right:
- Maintain the “maximum height among tall group students seen so far”
- For each student, if that maximum height is greater than their own height, they are a “looking-up student”
This determination is sufficient.
By maintaining information appropriately in this way, we can find the answer in a single scan.
Algorithm
- Read the input (using
sys.stdin.readfor faster I/O). - Initialize the variable
max_heightto-1(representing the state where no tall group member has appeared yet). - Scan students from left to right and do the following:
- If
max_height != -1andmax_height > S[i], then this person is a “looking-up student,” so increment the count. - If
S[i] >= T(tall group member), check and possibly updatemax_height.
- If
This yields the result in linear time.
Complexity
- Time complexity: \(O(N)\)
- Space complexity: \(O(1)\) (excluding input data)
Implementation Notes
The key is to efficiently maintain the “maximum height of tall group members before the current student” for each student.
The first student (student number 1) has no one before them, so they are unconditionally not a looking-up student.
Setting the initial value of
max_heightto-1or some other invalid value to represent that no tall group member has appeared yet is an important detail.Source Code
import sys
input = sys.stdin.read
def main():
data = input().split()
N = int(data[0])
T = int(data[1])
S = list(map(int, data[2:]))
max_height = -1
count = 0
for i in range(N):
if i > 0:
# 自分より前の高身長グループで最も高い人との比較
if max_height != -1 and max_height > S[i]:
count += 1
# 高身長グループであれば、最大身長を更新
if S[i] >= T:
if max_height == -1 or S[i] > max_height:
max_height = S[i]
print(count)
if __name__ == "__main__":
main()
This editorial was generated by qwen3-coder-480b.