当前位置:网站首页>【acwing】786. 第k个数
【acwing】786. 第k个数
2022-07-07 07:46:00 【percation】
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int q[N];
int n,k;
int qs(int l, int r, int k){
if(l == r) return q[l];
int i = l - 1, j = r + 1, x = q[(l + r)/2];
while(i < j){
while(q[++i] < x) ;//实现左区间向右边前进,右区间向左边前进
while(q[--j] > x) ;
if(i < j) swap(q[i],q[j]);
}
int s1 = j - l + 1;//找到当时分界点在区间的位置,因区间从0开始,所以要加1
if(k <= s1)//当所要查询的数,在当前分界点的左边时,则进行左递归,否则进行右递归。
return qs(l, j, k);
return qs(j + 1,r, k - s1);
}
int main(){
scanf("%d%d",&n,&k);
for(int i = 0; i < N; i++){
scanf("%d",&q[i]);
}
cout << qs(0,n-1,k) << endl;
return 0;
}
边栏推荐
猜你喜欢
ORM -- grouping query, aggregation query, query set queryset object properties
ORM model -- associated fields, abstract model classes
Enterprise practice | construction of banking operation and maintenance index system under complex business relations
ORM模型--关联字段,抽象模型类
The story of Plato and his three disciples: how to find happiness? How to find the ideal partner?
官媒关注!国内数字藏品平台百强榜发布,行业加速合规健康发展
Web3.0 series distributed storage IPFs
Agile course training
高数_第1章空间解析几何与向量代数_向量的数量积
Delete a record in the table in pl/sql by mistake, and the recovery method
随机推荐
官媒关注!国内数字藏品平台百强榜发布,行业加速合规健康发展
Parameter sniffing (1/2)
C socke server, client, UDP
China's first electronic audio category "Yamano electronic audio" digital collection is on sale!
Enterprise practice | construction of banking operation and maintenance index system under complex business relations
Use of JSON extractor originals in JMeter
Codeforces - 1324d pair of topics
The story of Plato and his three disciples: how to find happiness? How to find the ideal partner?
Mongodb creates an implicit database as an exercise
能源路由器入门必读:面向能源互联网的架构和功能
Weekly recommended short videos: what are the functions of L2 that we often use in daily life?
Wallys/IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL CONCURRENT
[learning notes - Li Hongyi] Gan (generation of confrontation network) full series (I)
ORM model -- associated fields, abstract model classes
Interface test
运用tensorflow中的keras搭建卷积神经网络
STM32基础知识—内存映射
The applet realizes multi-level page switching back and forth, and supports sliding and clicking operations
uboot机构简介
Applet sliding, clicking and switching simple UI