当前位置:网站首页>Variant quick platoon: find the largest number of top k
Variant quick platoon: find the largest number of top k
2022-04-22 08:19:00 【Xue Dongjing】
seek n The largest of the number K Number , Generally, you can think of selective sorting or heap sorting , Here is a method of using the idea of fast scheduling .
Fast scheduling each round is to divide a set of data into three parts , A number less than the reference number , Benchmark number , A number greater than or equal to the reference number . It is conceivable that when the number greater than or equal to the reference number is equal to K This part is the answer . We can use the idea of fast descending sorting , Because the fast platoon will determine the final position of a reference number at a time , Get the subscript of the reference number every time ( Subscript from 0 Start ) Then we'll compare it with k-1 Compare , When equal to , Explain that the reference number and the number to the left of the reference number are the first K Large number ( Note that these numbers are not in descending order ). When less than , It indicates that the datum and the number on the left are not enough K individual , However, it can be determined that the reference number and the number to the left of the reference number must be here K In number , So don't worry about the benchmark number and the number on his left , Repeat the above operation for the number on the right , Until you get the answer . When greater than , Indicates that the reference number and the number to the left of the reference number are more than k individual , Do the above operation for the number on the left , Until you get the answer . The average time complexity is twice as fast as that of fast queue O(nlogn), The worst time complexity is O(n^2).
Code
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;
void Qs(int low,int high,vector<int>&nums,int k)
{
int i=low,j=high,temp=nums[low];
while(i<j){
while(i<j&&nums[j]<temp){
j--;
}
nums[i]=nums[j];
while(i<j&&nums[i]>=temp){
i++;
}
nums[j]=nums[i];
}
nums[i]=temp;
if(j==k-1){
return ;
}else{
if(j>=k){
Qs(low,j-1,nums,k);
}else{
Qs(i+1,high,nums,k);
}
}
}
int main()
{
int n,k,x;
vector<int>nums;
nums.clear();
scanf("%d%d",&n,&k);
for(int i=0;i<n;i++){
scanf("%d",&x);
nums.push_back(x);
}
Qs(0,n-1,nums,k);
for(int i=0;i<k;i++){
if(i!=0){
printf(" ");
}
printf("%d",nums[i]);
}
return 0;
}
版权声明
本文为[Xue Dongjing]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220707183823.html
边栏推荐
- OpenFeign之响应处理
- 牛客白月赛5 【题解 数学场】
- Integrated service PDH optical transceiver 4-way Gigabit isolated Ethernet + 4-way E1 private network service 2m integrated service optical transceiver
- 4E1+2路千兆隔离网络+4路百兆物理隔离网络PDH光端机
- tf.keras.layers.Dense函数
- 荧光标记的透明质酸|FITC-Hyaluronate|荧光素透明质酸|Fluorescent hyaluronic acid
- 【速领】电子工程师入门必备计算工具-方波电路辅助设计表格
- PDH光端机4路E1+4路百兆以太网 4路2M光端机 FC单纤20公里 机架式
- Hanyuan hi tech 8-way multi service PDH optical transceiver double optical port protection + 8-way E1 + 4-way Gigabit Ethernet + 4-way 100m network electric port
- 网络原理二(上)
猜你喜欢

ACM入门之【容斥定理】

ADB advanced commands

牛客白月赛5 【题解 数学场】

16 way E1 optical transceiver + 4-way 100M Ethernet optical transceiver PDH optical transceiver 2m integrated service optical transceiver

Differences between routing modes

电信级双光口保护8E1+4路物理隔离千兆网络光端机1000M网络100M光端机

综合光接入设备4路百兆隔离以太网络+16路E1专网业务2M综合业务光端机

Sun Yuchen, founder of wave field Tron, announced that it will officially launch the decentralized stable currency usdd

tensorflow使用笔记

Fluorescently labeled hyaluronic acid
随机推荐
ER 和 EER 模型
CAS:36530-06-0氯化硼亚酞菁|亚酞菁|氯化硼亚酞菁|二氯硼酞菁染料|氯化亚酞菁硼|BORONSUBPHTHALOCYANINECHLORIDE
氨基(-NH2)酞菁铜 cas: 28632-30-6 (四氨基酞菁)铜(II) 四氨基酞菁铜(CuTAPc)的多种叫法-齐岳生物小编分享
16路E1光端机+4路百兆以太网络光端机PDH光端机2M综合业务光端机
氧化镁MgO晶体基片|钛酸锶SrTiO3晶体基片|铌酸锂LiNbO3晶体基片;直径10mm
MYSQL05_ORDR BY排序、LIMIT分组、GROUP BY分组
ADB advanced commands
The JMeter interface requests a security authentication solution
牛客白月赛5 【题解 数学场】
HLS / Chisel 利用CORDIC双曲系统实现平方根计算
Hanyuan hi tech 8-way multi service PDH optical transceiver double optical port protection + 8-way E1 + 4-way Gigabit Ethernet + 4-way 100m network electric port
pycharm终端pip安装Error: “pip”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
光纤传输16路E1+4路千兆隔离以太网络光端机2M专网千兆以太网综合多业务PDH光端机
tf.keras.layers.Embedding函数
通过OpenFeign传递对象类型参数
JMeter parameter request type
tf.keras.layers.InputLayer函数
汉源高科8路多业务PDH光端机双光口保护+8路E1+4路千兆以太网+4路百兆网络电口
Dynamic programming -- lc518 Change II
Carrier grade double optical port protection 8e1 + 4-way physical isolation gigabit network optical transceiver 1000m network 100m optical transceiver