当前位置:网站首页>AcWing 786. 第k个数
AcWing 786. 第k个数
2022-07-03 08:49:00 【Sasakihaise_】
【快排】按照快排的思想,直到中间分隔点=k
// package AcWing.course;
import java.io.StreamTokenizer;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Main {
// 7:50. 8:06
static int n, k, ans = 0;
static int[] arr;
static StreamTokenizer st;
static int nextInt() throws Exception {
st.nextToken();
return (int)st.nval;
}
static void quickSort(int l, int r){
if(l > r) return;
int i = l, j = r, pivot = arr[l];
while(i < j){
while(i < j && arr[j] > pivot) j--;
arr[i] = arr[j];
while(i < j && arr[i] <= pivot) i++;
arr[j] = arr[i];
}
arr[i] = pivot;
if(i == k) {
ans = arr[i];
return;
}else if(i > k){
quickSort(l, i - 1);
}else{
quickSort(i + 1, r);
}
}
public static void main(String[] args) throws Exception {
st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
PrintWriter pw = new PrintWriter(System.out);
n = nextInt();
k = nextInt() - 1;
arr = new int[n];
for(var i = 0; i < n; i++) arr[i] = nextInt();
quickSort(0, n - 1);
pw.println(ans);
pw.flush();
}
}
边栏推荐
猜你喜欢

LeetCode 241. 为运算表达式设计优先级

第一个Servlet

Monotonic stack -42 Connect rainwater

Binary tree sorting (C language, char type)

Try to reprint an article about CSDN reprint

分配异常的servlet
![[concurrent programming] consistency hash](/img/5e/3d0a52caa8ca489a6e6267274bbb39.jpg)
[concurrent programming] consistency hash

22-06-28 Xi'an redis (02) persistence mechanism, entry, transaction control, master-slave replication mechanism

Vscode connect to remote server

Dom4j traverses and updates XML
随机推荐
[concurrent programming] collaboration between threads
TP5 order multi condition sort
22-05-26 Xi'an interview question (01) preparation
too many open files解决方案
Deep parsing (picture and text) JVM garbage collector (II)
Introduction to the usage of getopts in shell
[rust notes] 02 ownership
[concurrent programming] working mechanism and type of thread pool
The method of replacing the newline character '\n' of a file with a space in the shell
C language student management system based on linked list, super detailed
Common DOS commands
第一个Servlet
How to delete CSDN after sending a wrong blog? How to operate quickly
Try to reprint an article about CSDN reprint
[concurrent programming] Table hopping and blocking queue
Dom4j traverses and updates XML
MySQL three logs
Mortgage Calculator
树形DP AcWing 285. 没有上司的舞会
Convert video to GIF