Less or Equal ——NEUQ-ACM Codeforces Round #479 (Div. 3)

地址:点击打开链接

我们先回顾下题目

C - Less or Equal

 

You are given a sequence of integers of length nn and integer number kk. You should print any integer number xx in the range of [1;109][1;109] (i.e. 1x1091≤x≤109) such that exactly kk elements of given sequence are less than or equal to xx.

Note that the sequence can contain equal elements.

If there is no such xx, print "-1" (without quotes).

Input

The first line of the input contains integer numbers nn and kk (1n21051≤n≤2⋅1050kn0≤k≤n). The second line of the input contains nn integer numbers a1,a2,,ana1,a2,…,an (1ai1091≤ai≤109) — the sequence itself.

Output

Print any integer number xx from range [1;109][1;109] such that exactly kk elements of given sequence is less or equal than xx.

If there is no such xx, print "-1" (without quotes).

Examples
Input
7 4
3 7 5 1 10 3 20
Output
6
Input
7 2
3 7 5 1 10 3 20
Output
-1
Note

In the first example 55 is also a valid answer because the elements with indices [1,3,4,6][1,3,4,6] is less than or equal to 55 and obviously less than or equal to 66.

扫描二维码关注公众号,回复: 2682335 查看本文章

In the second example you cannot choose any number that only 22 elements of the given sequence will be less than or equal to this number because 3

3 elements of the given sequence will be also less than or equal to this number.


这道题的精髓就是注意数据的边界问题。

要注意的坑:当k=0时和k=n时,需要额外讨论!最后就是判断普通情况下不能取到x的值的情况。


代码如下:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    int num[n];
    for(int i=0;i<n;++i)
    {
        scanf("%d",&num[i]);
    }
    sort(num,num+n);
    if(k==0)
    {
        if(num[0]>=2) printf("%d\n",num[0]-1);
        else printf("-1\n");
        return 0;
    }
    if(k==n)
    {
        printf("%d\n",num[n-1]);
        return 0;
    }

    if(num[k]==num[k-1])
        printf("-1\n");
    else printf("%d\n",num[k]-1);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jtjljy/article/details/80304088