320

2
8 5
10 9 8 7 7 7 5 5
4 2
0 0 0 0

6
0

# 题解

lower_bound()upper_bound() 虽然非常快,但是反而出错几率会变大

# 代码

`cpp 晋级下一轮 https://github.com/OhYee/sourcecode/tree/master/ACM 代码备份
/*/
#define debug
#include
//*/
#include
#include
#include
#include
using namespace std;

const int maxn = 105;
int a[maxn];

typedef int LL;

int lowerbound(LL arr,int size, LL key) {
int half;
int mid;
int first = 0;
while (size > 0) {
half = size >> 1;
mid = first + half;
if (arr[mid] > key) {
first = mid + 1;
size = size - half - 1;
} else {
size = half;
}
}
return first;
}
int upperbound(LL arr,int size, LL key) {
int half;
int mid;
int first = 0;
while (size > 0) {
half = size >> 1;
mid = first + half;
if (arr[mid] >= key) {
first = mid + 1;
size = size - half - 1;
} else {
size = half;
}
}
return first;
}

int main(){
#ifdef debug
freopen("in.txt", "r", stdin);
int START = clock();
#endif
cin.tie(0);
cin.syncwithstdio(false);

int T;
cin >> T;
while(T--){
int n,k;
cin >> n >> k;
for(int i=0;icin >> a[i];

k = a[k-1];
if(!k)
cout << lower_bound(a,n,k) << endl;
else
cout << upper_bound(a,n,k) << endl;
}

#ifdef debug
printf("Time:%.3fs.n", double(clock() - START) / CLOCKSPERSEC);
#endif
return 0;
}

• 点击查看/关闭被识别为广告的评论