#include <algorithm> #include <cstdio> #include <functional> #include <vector> int main() { int n, k; scanf("%d%d", &n, &k); std::vector<int> a(n); for(int i = 0; i < n; ++i) { scanf("%d", &a[i]); } std::sort(a.begin(), a.end(), std::greater<int>()); int used = k; int kth = a[k-1]; for(int i = k; i < n; ++i) { if(a[i] == kth) ++used; else break; } printf("%d\n", used); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #include <algorithm> #include <cstdio> #include <functional> #include <vector> int main() { int n, k; scanf("%d%d", &n, &k); std::vector<int> a(n); for(int i = 0; i < n; ++i) { scanf("%d", &a[i]); } std::sort(a.begin(), a.end(), std::greater<int>()); int used = k; int kth = a[k-1]; for(int i = k; i < n; ++i) { if(a[i] == kth) ++used; else break; } printf("%d\n", used); } |