Less or Equal(CF-977C)
Problem Description
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≤10^9) 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 nn and kk (1≤n≤2?10^5, 0≤k≤n). The second line of the input contains n integer numbers a1,a2,…,an (1≤ai≤10^9) — 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
題意: 給出 n 個數,求是否存在一個整數使得這個 n 個數中的 k 個數小于或等于這個整數,如果有這個整數,輸出這個整數,如果沒有,輸出 -1
思路:一開始以為用 sort 排序后直接輸出第 k 個即可,后來發現沒這么簡單,當 k 大于 1 或小于 n 時是可以的,但當 k 等于 1 或等于 n 時,情況就不同了,因此在 n 個數兩端,根據要求的[1,1e9],加上一個最大值與最小值,再 sort 排序即可。
Source Program
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<string> #include<cstdlib> #include<queue> #include<set> #include<map> #include<stack> #include<ctime> #include<vector> #define INF 0x3f3f3f3f #define PI acos(-1.0) #define N 1000005 #define MOD 1e9+7 #define E 1e-6 typedef long long ll; using namespace std; int a[N]; int main() {int n,k;cin>>n>>k;for(int i=1;i<=n;i++)cin>>a[i];a[0]=1;sort(a,a+n+1);a[n+2]=INF;if(a[k]==a[k+1])cout<<-1<<endl;elsecout<<a[k]<<endl;return 0; }?
總結
以上是生活随笔為你收集整理的Less or Equal(CF-977C)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 国王游戏(洛谷-P1080)
- 下一篇: 数的划分(信息学奥赛一本通-T1304)