B - 奨学金選考 / Scholarship Selection 解説 by admin
GPT 5.2 HighOverview
This problem asks you to determine whether each student scores at least the minimum threshold in every subject, collect those who pass the criteria, and then output (in ascending order) the student numbers of the top \(K\) students by total score (including all students tied at the \(K\)-th rank).
Analysis
First, the condition for “passing the criteria” is \(S_{i,j} \ge T_j\) for all subjects, so we can check this by examining all \(M\) subjects for each student (\(M \le 10\), so this is sufficiently lightweight).
Next, there is a subtle point in how recipients are determined:
- When the students who pass the criteria are sorted in descending order of total score
- We take the “top \(K\) students,” but if there are students tied with the \(K\)-th ranked student, all of them are included → In other words, “all students who pass the criteria and have a total score greater than or equal to the \(K\)-th ranked total score are selected.”
A naive approach of “sort all qualifying students and take only the first \(K\)” will result in WA if tie-breaking is forgotten.
Example: If total scores are [100, 90, 80, 80, 70] and \(K=3\), the 3rd rank has a score of 80 with a tie, so 4 students are selected, including both students with 80 points.
This tie handling can be implemented concisely by “finding the total score at the \(K\)-th rank (the boundary value) and selecting everyone with a score greater than or equal to that value.”
Algorithm
- Read the input.
- For each student \(i\):
- Check whether \(S_{i,j} \ge T_j\) for all subjects (disqualified if even one subject falls below the threshold).
- Simultaneously compute the total score \(sum_i = \sum_{j=1}^{M} S_{i,j}\).
- Add
(sum_i, i)to the arrayqualifiedonly for students who meet the criteria.
- Let \(Q\) be the number of people in
qualified.- If \(Q = 0\), output nothing.
- If \(Q \le K\), select all qualifying students (output their numbers in ascending order).
- If \(Q > K\):
- Sort
qualifiedin descending order of total score. - Obtain the total score at the \(K\)-th rank (index
K-1in 0-indexed) askth_sum. - Collect all students with a total score of
kth_sumor higher, and output their student numbers in ascending order.
- Sort
Complexity
- Time complexity: Criteria checking and total computation take \(O(NM)\), and sorting the \(Q\) qualifying students takes \(O(Q \log Q)\). Overall: \(O(NM + Q \log Q)\) (sufficiently fast since \(M \le 10\)).
- Space complexity: \(O(Q)\) (to store
(total score, number)for qualifying students).
Implementation Notes
Since \(N \le 10^5\), in Python it is stable and fast to read all input at once using
sys.stdin.buffer.read().For tie handling, determining the \(K\)-th ranked total score
kth_sumand selecting everyone withtotal >= kth_sumensures no one is missed.The output must be in “ascending order of student numbers,” so sort only the selected student numbers with
sorted()at the end and output them separated by newlines.When there are 0 qualifying students, the specification is to output nothing, so
returnearly.Source Code
import sys
def main():
data = list(map(int, sys.stdin.buffer.read().split()))
it = iter(data)
N = next(it)
M = next(it)
K = next(it)
T = [next(it) for _ in range(M)]
qualified = []
for i in range(1, N + 1):
total = 0
ok = True
for j in range(M):
s = next(it)
if s < T[j]:
ok = False
total += s
if ok:
qualified.append((total, i))
qn = len(qualified)
if qn == 0:
return
if qn <= K:
ans = sorted(idx for _, idx in qualified)
else:
qualified.sort(key=lambda x: x[0], reverse=True)
kth_sum = qualified[K - 1][0]
ans = sorted(idx for total, idx in qualified if total >= kth_sum)
sys.stdout.write("\n".join(map(str, ans)))
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.2-high.
投稿日時:
最終更新: