当前位置:网站首页>Acwing786. The kth number
Acwing786. The kth number
2022-07-19 12:29:00 【Science and technology big pig】
Given a length of n The whole number sequence of , And an integer k, Please use the quick selection algorithm to find out the number sequence from small to large k Number .
Input format
The first line contains two integers n and k.
The second line contains n It's an integer ( All integers are in 1∼10^9 Within the scope of ), Represents an integer sequence .
Output format
Output an integer , It represents the second part of a sequence k decimal .
Data range
1≤n≤100000,
1≤k≤n
sample input :
5 3
2 4 1 5 3
sample output :
3It's the fast platoon , Focus on mastering the core of fast scheduling algorithm , The core idea of partition
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010;
int q[N];
void quick_sort(int q[], int l, int r)
{
if (l >= r) return;
int i = l - 1, j = r + 1, x = q[l + r >> 1];
while (i < j)
{
do {
i ++;
}while (q[i] < x);
do{
j -- ;
}while (q[j] > x);
if (i < j) swap(q[i], q[j]);
}
quick_sort(q, l, j);
quick_sort(q, j + 1, r);
}
int main()
{
int n , k;
scanf("%d%d", &n , &k);
for (int i = 0; i < n; i ++ ) scanf("%d", &q[i]);
quick_sort(q, 0, n - 1);
for (int i = 0; i < n; i ++ ) if(i == k - 1)printf("%d ", q[i]);
return 0;
}
边栏推荐
猜你喜欢

Familiar with nestjs (beginner)

Es install IK word breaker

Example of C language drawing - 20 examples of chromatic diagram

Yunxi and Tencent cloud have reached a strategic cooperation to accelerate the expansion of the global live broadcast market

脉冲函数、阶跃函数和斜坡函数及脉冲响应

HCIP(4)
MIHA tour 2023 autumn recruitment officially begins ~ early approval has the opportunity to avoid written examination!

2022-07-07:Spire. Office 7.7.2 for net debuted

Core base station_ The error "no gateways configured" is reported when starting the CPA file

MySQL learning notes - constraints
随机推荐
MIHA tour 2023 autumn recruitment officially begins ~ early approval has the opportunity to avoid written examination!
Pytorch version: yolov4 integrating attention and mobilenet
【C语言编程7】BTB模型
HCIP (7)
Database daily question --- day 25: bank account summary II
Array de duplication array sorting maximum sum class array conversion filter array
[shutter] dart: some features that cannot be ignored
Nature | the carbon sequestration rate of groundwater is similar to that of oligotrophic marine system
mysql中对于数据库的基本操作
What is the relationship between softmax and cross enterprise?
Relationship and difference between wav and PCM
The leader of the new generation of cloud database -- AWS
Core base station_ The error "no gateways configured" is reported when starting the CPA file
C # from introduction to mastery Part 1: C # overview and introduction
Configuring OSPF experiment in mGRE environment
第四天作業
Es install IK word breaker
阿趣的思考
JS chain call sleep function ------ "autumn trick punch in Chapter 2"
招生宣传-江南大学