链接:https://vjudge.net/problem/CodeForces-977C
题目
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;10^9] (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).
题意
给出n个数,让你求一个数x使得在序列中有k个元素小于等于x。
x的范围:0<x<1e9
思路
一道思维题,先对序列由小到大排序,然后分情况讨论:
- 如果k==0,就看a[1],如果a[1]==1,就输出-1;否则输出a[1]-1。
- 否则就看a[k],如果a[k]==a[k+1],输出-1(稍微想一下);否则输出a[k](再想一下)。
看代码,伊丽莎白!
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+100;
int a[maxn];
int main()
{
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++)
cin>>a[i];
sort(a+1,a+1+n);
if(k==0)
{
if(a[1]==1)
cout<<-1<<endl;
else
cout<<a[1]-1<<endl;
}
else
{
if(a[k]==a[k+1])
cout<<-1<<endl;
else
cout<<a[k]<<endl;
}
}
最近就时常写一写,考试还是要准备的(lll¬ω¬)