/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int last(vector<int> a, int n, int x)
{
int l = 0, r = n - 1;
int res = -1;
while (l <= r){
int m = (l + r)/2;
if (x == a[m]){
res = m;
l = m + 1;
}else if (x < a[m]) {
l = m + 1;
}else {
r = m - 1;
}
}
return res;
}
int main()
{
int n, k;
cin >> n >> k;
vector<int> a;
for(int i=0; i<n; i++){
int x;
cin >> x;
a.push_back(x);
}
sort(a.begin(), a.end(), greater<int>());
int last_pos = last(a, n, a[k-1]);
cout << last_pos+1;
}
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | /****************************************************************************** Welcome to GDB Online. GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl, C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog. Code, Compile, Run and Debug online from anywhere in world. *******************************************************************************/ #include <iostream> #include <algorithm> #include <vector> using namespace std; int last(vector<int> a, int n, int x) { int l = 0, r = n - 1; int res = -1; while (l <= r){ int m = (l + r)/2; if (x == a[m]){ res = m; l = m + 1; }else if (x < a[m]) { l = m + 1; }else { r = m - 1; } } return res; } int main() { int n, k; cin >> n >> k; vector<int> a; for(int i=0; i<n; i++){ int x; cin >> x; a.push_back(x); } sort(a.begin(), a.end(), greater<int>()); int last_pos = last(a, n, a[k-1]); cout << last_pos+1; } |
English