CodeForces - 977C:Less or Equal(思维)

39 篇文章 1 订阅
10 篇文章 0 订阅

链接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

思路
一道思维题,先对序列由小到大排序,然后分情况讨论:

  1. 如果k==0,就看a[1],如果a[1]==1,就输出-1;否则输出a[1]-1。
  2. 否则就看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¬ω¬)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值