Codeforces Round #479 (Div. 3) C. Less or Equal

题目地址:http://codeforces.com/contest/977/problem/C

题解:给一串数组,是否找到一个数x,找到k个数字<=x,找到输出x,不能输出-1。例如第二组,要找到两个数字,排序后出现1,3,3,会出现三个数字小于等于3,所以不能找到。

           一个坑点,k=0的时候需要分类讨论,如果发现最小的数字是1的话,不能输出0,因为要求输出1~1e9之间的数,否则输出a[0]-1就可以啦~

方法:排序以后分类判断即可。

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<iostream>
#include<map>
#include<vector>
#include<set>
#include<queue>
using namespace std;
int a[250000];
int main()
{
    int n, k;
    cin >> n >> k;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    sort(a, a + n);
    if (k == 0)
    {
        if (a[0] == 1)
            printf("-1
");
        else
            printf("%d
", a[0] - 1);
    }
    else if (k == n)
    {
        printf("%d
", a[n - 1]);
    }
    else
    {
        if (a[k - 1] == a[k])
        {
            printf("-1
");
        }
        else
        {
            printf("%d
", a[k - 1]);
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/Tangent-1231/p/9005443.html