牛客:第k小数题解

题目描述

给你一个长度为n的序列,求序列中第k小数的多少。

输入描述

多组输入,第一行读入一个整数T表示有T组数据。

每组数据占两行,第一行为两个整数n,k,表示数列长度和k。

第二行为n个用空格隔开的整数。

输出描述:

对于每组数据,输出它的第k小数是多少。

每组数据之间用空格隔开

示例


输入

2
5 2
1 4 2 3 4
3 3
3 2 1

输出

2
3

备注:
 

t≤10,1≤n≤5×106,k≤n,数列里每个数都在int范围内

由于输入比较多,请使用快读读取。

例如:

inline int read(){
    int x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9'){
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9'){
        x = (x<<1) + (x<<3) + (ch^48);
        ch = getchar();
    }
    return x * f;
}

代码如下

#include<iostream>
#include<algorithm>
using namespace std;

const int N = 1e6 + 10;
int arr[N];

inline int read() {
    int x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    return x * f;
}
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n, k;
        n = read(), k = read();
        for (int i = 0; i < n; i++)
        {
            arr[i] = read();
        }
        sort(arr, arr + n);
        cout << arr[k - 1] << endl;
	}
	return 0;
}

​​​​​​t原题链接:https://ac.nowcoder.com/acm/problem/207028https://ac.nowcoder.com/acm/problem/207028

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

清风青筝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值