time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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. 1≤x≤1091≤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 (1≤n≤2⋅1051≤n≤2⋅105, 0≤k≤n0≤k≤n). The second line of the input contains nn integer numbers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤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 to xx.
If there is no such xx, print "-1" (without quotes).
Examples
input
Copy
7 4 3 7 5 1 10 3 20
output
Copy
6
input
Copy
7 2 3 7 5 1 10 3 20
output
Copy
-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.
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 33 elements of the given sequence will be also less than or equal to this number.
算法分析:
题意:
给你n个数,叫你求出x,满足有k个数小于等于x。
分析:
水坑啊,就四点,k可以为0,1=<x<=1e9,a[i]>=1,x=-1表示没有情况(-1已经不是数了)。
举例说明把,
3 0
2 2 3
则x=1满足情况,这时候不能x=-1,x的范围
3 0
1 1 3
这时候,只能输出-1
代码实现:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
using namespace std;
const double eps = 1e-8;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN=5010;
const int MAXM=100010;
using namespace std;
int a[200005];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+n+1);
if(m==0)
{
if(a[1]>1)
cout<<1<<endl;
else
cout<<"-1"<<endl;
continue;
}
int flag=0;
if(a[m+1]==a[m])
{flag=1; }
if(flag==1)
cout<<"-1"<<endl;
else
cout<<a[m]<<endl;
}
return 0;
}