B - プログラミングコンテストの表彰 / Awards of a Programming Contest 解説 by admin 印刷ページ
GPT 5.2 HighOverview
This problem asks you to sort the scores in descending order, use the \(K\)-th score \(B_K\) as a reference (threshold), and count the number of original participants whose score is at least \(B_K\).
Analysis
The award criteria is not simply “the top \(K\) people,” but rather “include everyone who has the same score as the person in \(K\)-th place.” Therefore, what matters is not the rank itself, but the \(K\)-th score value (threshold).
For example, if the scores sorted in descending order are: - \([100, 90, 90, 80, 70]\), \(K=2\)
Here \(B_2 = 90\), so the award recipients are those with scores “\(90\) or above” = \(100, 90, 90\), which is 3 people. Simply outputting \(K=2\) people would be incorrect (WA).
While it is possible to naively “rank all participants and count while handling ties,” the essence can be decomposed into two steps: 1. Find \(B_K\) (the \(K\)-th score) 2. Count the number of elements in the original array that are at least \(B_K\)
This formulation makes the implementation concise and reduces the chance of mistakes.
Algorithm
- Create an array \(A\_sorted\) by sorting the score array \(A\) in descending order.
- Extract the threshold \(threshold = A\_sorted[K-1]\) (using 0-indexing, so \(K-1\)). This corresponds to \(B_K\).
- Count and output the number of elements in the original array \(A\) satisfying \(A_i \ge threshold\).
The key point is that by using the \(K\)-th score obtained from sorting as a “threshold,” all tied participants are naturally included in the count.
Complexity
- Time complexity: \(O(N \log N)\) (dominated by the descending sort; counting is \(O(N)\))
- Space complexity: \(O(N)\) (for the sorted array, etc.)
Implementation Notes
The \(K\)-th element uses 1-indexing, so be careful to use
K-1with Python’s 0-indexed arrays.When counting using the threshold, you can scan the original array and count elements
>= threshold(the result is the same either way, but counting from the original array makes the intent clearer).Since the input size can be large, reading all input at once with
sys.stdin.buffer.read()is faster.Source Code
import sys
def main():
data = list(map(int, sys.stdin.buffer.read().split()))
N, K = data[0], data[1]
A = data[2:]
A_sorted = sorted(A, reverse=True)
threshold = A_sorted[K - 1]
ans = sum(1 for x in A if x >= threshold)
print(ans)
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.2-high.
投稿日時:
最終更新: