当前位置:网站首页>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();
}
}
边栏推荐
- Servlet的生命周期
- On the setting of global variable position in C language
- too many open files解决方案
- 22-05-26 Xi'an interview question (01) preparation
- 22-06-28 Xi'an redis (02) persistence mechanism, entry, transaction control, master-slave replication mechanism
- Mortgage Calculator
- MySQL index types B-tree and hash
- Deep parsing (picture and text) JVM garbage collector (II)
- Es8 async and await learning notes
- 剑指 Offer II 091. 粉刷房子
猜你喜欢

Es8 async and await learning notes

Binary tree traversal (first order traversal. Output results according to first order, middle order, and last order)

请求参数的发送和接收

樹形DP AcWing 285. 沒有上司的舞會

单调栈-42. 接雨水

Concurrent programming (V) detailed explanation of atomic and unsafe magic classes

Tree DP acwing 285 A dance without a boss

Binary tree sorting (C language, char type)

LeetCode 75. 颜色分类

Servlet的生命周期
随机推荐
Deeply understand the underlying data structure of MySQL index
Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial
Development experience and experience
第一个Servlet
Monotonic stack -84 The largest rectangle in the histogram
【Rust 笔记】10-操作符重载
Character pyramid
[RPC] RPC remote procedure call
分配异常的servlet
Parameters of convolutional neural network
C language file reading and writing
PHP uses foreach to get a value in a two-dimensional associative array (with instances)
Unity multi open script
树形DP AcWing 285. 没有上司的舞会
Notes and bugs generated during the use of h:i:s and y-m-d
TP5 order multi condition sort
How to place the parameters of the controller in the view after encountering the input textarea tag in the TP framework
Eating fruit
[concurrent programming] concurrent tool class of thread
DOM render mount patch responsive system