公式

B - プログラミングコンテストの表彰 / Awards of a Programming Contest 解説 by admin

Claude 4.6 Opus (Thinking)

Overview

When scores are sorted in descending order, the problem asks you to count how many participants have a score greater than or equal to the \(K\)-th score. The key point is that all participants tied at that score are also awarded.

Analysis

Let’s first organize the problem. The rule is: “Award the top \(K\) people, but also award everyone who has the same score as the person in \(K\)-th place.”

Let’s think through a concrete example.

For instance, if \(N = 6\), \(K = 3\), and the scores are [100, 90, 80, 80, 70, 60]:

  • Sorting in descending order gives \(B = [100, 90, 80, 80, 70, 60]\)
  • The \(K = 3\)-rd score is \(B_3 = 80\)
  • The people with scores \(\geq 80\) are \(100, 90, 80, 80\)4 people
  • Therefore, 4 people are awarded (more than \(K = 3\))

As we can see, we just need to find the \(K\)-th place score (the borderline) and count all people whose scores are greater than or equal to it.

Are there any issues with a straightforward approach?

In this problem, we simply sort, extract the \(K\)-th value, and count the number of people with scores at or above it, so there are no pitfalls that would cause TLE. The heaviest operation is sorting at \(O(N \log N)\), but since \(N \leq 2 \times 10^5\), this is fast enough.

Algorithm

  1. Sort the score array \(A\) in descending order (largest first).
  2. Take the \(K\)-th value in the sorted array (\(K-1\)-th in 0-indexed) as the borderline \(threshold\).
  3. Count the number of elements in array \(A\) that are greater than or equal to \(threshold\).
  4. That count is the answer.

Step-by-step example (\(N=5, K=2, A=[50, 80, 80, 30, 90]\)):

  1. Sort in descending order → \([90, 80, 80, 50, 30]\)
  2. \(K=2\)-nd element (index \(1\) in 0-indexed) → \(threshold = 80\)
  3. Elements \(\geq 80\): \(90, 80, 80\)3 elements
  4. Answer: 3

Complexity

  • Time complexity: \(O(N \log N)\) (dominated by sorting)
  • Space complexity: \(O(N)\) (to store the score array)

Implementation Notes

  • Since Python list indices are 0-indexed, note that the \(K\)-th score is A[K - 1].

  • Use A.sort(reverse=True) to sort in descending order. Using sorted() is also fine.

  • For counting the number of awarded participants, using a generator expression like sum(1 for a in A if a >= threshold) is a clean approach. Of course, using a for loop with a counter variable works just as well.

    Source Code

N, K = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)
threshold = A[K - 1]
print(sum(1 for a in A if a >= threshold))

This editorial was generated by claude4.6opus-thinking.

投稿日時:
最終更新: