Codeforces #479 (Div. 3)-C. Less or Equal-
You are given a sequence of integers of length n and integer number k. You should print any integer number x in the range of [1;109] (i.e. 1≤x≤109) such that exactly k elements of given sequence are less than or equal to x.
Note that the sequence can contain equal elements.
If there is no such x, print “-1” (without quotes).
Input
The first line of the input contains integer numbers n and k (1≤n≤2⋅105, 0≤k≤n). The second line of the input contains n integer numbers a1,a2,…,an (1≤ai≤109) — the sequence itself.
Output
Print any integer number x from range [1;109] such that exactly k elements of given sequence is less or equal to x.
If there is no such x, 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 5 is also a valid answer because the elements with indices [1,3,4,6] is less than or equal to 5 and obviously less than or equal to 6.
In the second example you cannot choose any number that only 2 elements of the given sequence will be less than or equal to this number because 3 elements of the given sequence will be also less than or equal to this number.
题意:
1~10的九次方 范围内的任何整数x(即1≤x≤10^9),使得给定序列的恰好k个元素小于或等于x。
让你找出是否存在x;如果不存在则输出-1;
情况是:
1.当第排序好后的第k-1个与第k个元素相等时必然不满足 否则输出a[k-1]即可;
2.当k等于0 时 给定的序列里如果a[0]等于1 必然不存在;否则 直接输出1 即可;
3.当k==n时 直接输出a[k-1]即可;
解题 排序 考虑所有出现的情况 即可
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdio>
#define ll long long
using namespace std;
const int64_t MM=1e6+5;
int n,m;
int a[MM];
int main()
{
scanf("%d %d",&n,&m);
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
sort(a,a+n);//排序
if(m==0)
{
if(a[0]==1)
printf("-1\n");
else
cout<<"1"<<endl;
}
else if(m==n)
{
cout<<a[m-1]<<endl;
}
else {
if(a[m]==a[m-1])
cout<<"-1"<<endl;
else
cout<<a[m-1]<<endl;
}
return 0;
}