公式

B - プログラミングコンテストの表彰 / Awards of a Programming Contest 解説 by kyopro_friends 印刷ページ


まず \(A\) をソートすることで \(K\) 位のスコアを求め、その後そのスコア以上の人が何人いるかを実際に調べればよいです。

計算量は \(O(N\log N)\) です。

実装例 (C++)

#include<bits/stdc++.h>
using namespace std;

int main(){
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for(int i=0; i<n; i++) cin >> a[i];
  sort(a.rbegin(), a.rend());

  int ans = 0;
  int threshold = a[k-1];
  for(int i=0; i<n; i++){
    if(a[i] >= threshold){
      ans++;
    }
  }
  cout << ans << endl;
}

実装例 (Python)

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

投稿日時:
最終更新: