B - 効率の良いクエスト / Efficient Quests Editorial by admin
GPT 5.2 HighOverview
This is a problem where you determine whether each quest satisfies the “efficiency condition” \(R_i \ge K \times D_i\), then sum up the experience points \(R_i\) of those that satisfy it and check whether the total is at least \(T\).
Analysis
The key point is that in this problem, there is no need to optimize “which quests to choose.” Takahashi has already decided to clear all and only the efficient quests, so there are only two things to do:
- For each quest, check whether the condition \(R_i \ge K \times D_i\) is satisfied
- Sum up \(R_i\) of the quests that satisfy the condition, and determine whether the total is at least \(T\)
Even with a straightforward approach, a full scan (examining all \(N\) quests sequentially) is sufficient. However, since the constraint is as large as \(N \le 2 \times 10^5\), unnecessary operations (such as sorting, DP, combinatorial search, etc.) would slow things down or make the implementation heavier. This problem can be solved in a single pass, so it can be handled with just a loop of \(N\) iterations.
Additionally, once the total experience reaches \(T\) or more, there is no need to read further. By outputting Yes and terminating midway, you can further reduce execution time (early termination).
Concrete examples: - If \(K=3\) and a quest has \((D,R)=(4,12)\), then \(12 \ge 3 \times 4\), so it qualifies for clearing - If \((D,R)=(5,14)\), then \(14 \not\ge 15\), so it does not qualify We just sum up the \(R\) values of qualifying quests and check whether the total reaches \(T\).
Algorithm
- Read \(N, K, T\) from input
- Initialize
total = 0 - For \(i=1..N\), repeat the following:
- Read \(D_i, R_i\)
- If \(R_i \ge K \times D_i\), then
total += R_i - If
total >= T, outputYesand terminate (early termination)
- After the loop ends, output
Yesiftotal >= T, otherwise outputNo
Complexity
- Time complexity: \(O(N)\) (just checking each quest once)
- Space complexity: \(O(1)\) (only a constant number of variables such as the running total)
Implementation Notes
Since \(T \le 10^{18}\) can be very large, the total can also become large. However, Python integers do not overflow, so this is safe (in other languages, use 64-bit integers).
Since the input can have up to \(2 \times 10^5\) lines, reading with
sys.stdin.buffer.read()for fast input is recommended for stability.By returning
Yesthe momenttotal >= Tis reached, you can speed things up in average cases.Source Code
import sys
def main():
it = iter(sys.stdin.buffer.read().split())
N = int(next(it))
K = int(next(it))
T = int(next(it))
total = 0
for _ in range(N):
D = int(next(it))
R = int(next(it))
if R >= K * D:
total += R
if total >= T:
print("Yes")
return
print("Yes" if total >= T else "No")
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.2-high.