当前位置:网站首页>Acwing786. 第k个数
Acwing786. 第k个数
2022-07-17 16:28:00 【理工大猪猪】
给定一个长度为 n 的整数数列,以及一个整数 k,请用快速选择算法求出数列从小到大排序后的第 k 个数。
输入格式
第一行包含两个整数 n 和 k。
第二行包含 n 个整数(所有整数均在 1∼10^9 范围内),表示整数数列。
输出格式
输出一个整数,表示数列的第 k 小数。
数据范围
1≤n≤100000,
1≤k≤n
输入样例:
5 3
2 4 1 5 3
输出样例:
3就是用到了快排,重点掌握好快排算法的核心,分治的核心思想
#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;
}
边栏推荐
- MIHA tour 2023 autumn recruitment officially begins ~ early approval has the opportunity to avoid written examination!
- Day 4 homework
- Travail du quatrième jour
- 01 knapsack interview questions series (I)
- C语言绘图示例-商标徽标
- 七月集训(第17天) —— 广度优先搜索
- Research and implementation of 5g network Slicing Based on AI intelligent correlation technology
- HCIP(4)
- Overview of the application of air, space and sea Association
- 若依excel合并单元格导出ExcelUtils
猜你喜欢

Genesis and bluerun ventures have in-depth exchanges

Configuring OSPF experiment in mGRE environment

如何应用小程序容器技术开发Hybrid App

C语言绘图示例-繁花图案

C语言绘图示例-调色板

C语言绘图示例-分色调图20例

C language drawing example - trademark logo

Energy saving resource allocation strategy based on time-varying graph in satellite networks

Microcomputer principle and technology Interface Experiment four subroutines and interrupt experiment

RAID 磁盘阵列详解,RAID分类及优缺点
随机推荐
OpenCV 教程 03: 如何跟踪视频中的某一对象
Linux下MySQL的安装与使用
PPPoE dial up
Machine learning Assignment 1
mysql如何删除数据表,被关联的数据表如何删除呢
Nature | the carbon sequestration rate of groundwater is similar to that of oligotrophic marine system
hcip第二天笔记
Example of C language drawing - 20 examples of chromatic diagram
Conversion between Swift binary data and hexadecimal string
Installation and use of MySQL under Linux
文件一键备份
Leetcode 239. 滑动窗口最大值
String correlation function (II)
Mysql-1366 - Incorrect string value: ‘\xE5\xBC\xA0\xE4\xB8\x89‘ for column ‘userName‘ at row 1
Leetcode 150. Evaluation of inverse Polish expression
如何运行.sh脚本文件
C语言绘图示例-调色板
渐变色按钮功能按钮绘制C语言示例
HCIP(4)
Solution: function RGB is missing argument $green Problems of