A - 遊園地のアトラクション / Amusement Park Attraction Editorial by admin
GPT 5.2 HighOverview
For the entire group to be able to ride, “even the shortest person must satisfy the condition,” so we count the number of rideable attractions based on the minimum height.
Analysis
The condition for member \(j\) to ride attraction \(i\) is \(S_i \le L_j\).
For “everyone to be able to ride,” this inequality must hold for all members.
The key observation is as follows:
- Everyone can ride \(\Leftrightarrow\) Even the shortest person (minimum height) can ride
Because, letting \(L_{\min}=\min(L_1,\dots,L_N)\):- If \(S_i \le L_{\min}\), then since all other members have \(L_j \ge L_{\min}\), everyone can ride
- If \(S_i > L_{\min}\), the shortest person cannot ride, so not everyone can ride
Therefore, the answer is “the number of attractions satisfying \(S_i \le L_{\min}\).”
Naively “checking all members for each attraction” would require up to \(M \times N \le 4 \times 10^{10}\) comparisons in the worst case, which is far too slow under the given constraints (TLE).
Once we realize we only need to check the minimum height, the number of comparisons reduces to \(M\).
Example:
- Heights \(L=[150, 170, 160]\) → \(L_{\min}=150\)
- Restrictions \(S=[140, 150, 151, 130]\): those \(\le 150\) are \(140, 150, 130\), giving 3
Therefore, the answer is 3
Algorithm
- Find the minimum value \(L_{\min}\) of the members’ height array \(L\).
- For the attractions’ restriction array \(S\), count the number of elements satisfying \(S_i \le L_{\min}\).
- Output that count.
Complexity
- Time complexity: \(O(N+M)\) (\(O(N)\) for computing the minimum, \(O(M)\) for counting)
- Space complexity: \(O(1)\) (constant additional memory aside from the input arrays)
Implementation Notes
The core insight is that the condition “everyone can ride” can be determined using only the minimum height.
Since the input can be as large as \(2 \times 10^5\), in Python it is faster to read all input at once using
sys.stdin.buffer.read().The count can be written concisely as
sum(1 for x in S if x <= min_height).Source Code
import sys
def main():
data = list(map(int, sys.stdin.buffer.read().split()))
N, M = data[0], data[1]
L = data[2:2+N]
S = data[2+N:2+N+M]
min_height = min(L)
ans = sum(1 for x in S if x <= min_height)
print(ans)
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.2-high.
posted:
last update: