K-th Number(二分答案+尺取法判断)

我写的,第二个样例不对,感觉按照题目说的写出来

#include <bits/stdc++.h>

using namespace std;
const int maxn = 1e5 + 10;
int t,n,m,k,a[maxn],b[maxn];
int c;
int main() {
    //freopen("in","r",stdin);
    ios::sync_with_stdio(0);
    cin >> t;
    while (t--) {
        cin >> n >> k >> m;
        c = 0;
        for (int i = 1; i <= n; i++) {
            cin >>  a[i];
            if(i > 1 ){
                if(abs(a[i] - a[i - 1]) >= k){
                    if(a[i] > a[i - 1])
                        b[++c] = a[i - 1] + k - 1;
                    else  b[++c] = a[i] + k - 1;
                }
            }
        }
        sort(b+1,b+1+c);
        if(c > m) cout << b[m] << endl;
        else cout << b[c] << endl;
    }

    return 0;
}
View Code

https://ac.nowcoder.com/acm/problem/14301

https://blog.csdn.net/lzc504603913/article/details/78514649

https://blog.csdn.net/codeswarrior/article/details/82827902

原文地址:https://www.cnblogs.com/xcfxcf/p/12696764.html