当前位置:网站首页>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();
}
}
边栏推荐
- [rust notes] 06 package and module
- Slice and index of array with data type
- Unity editor expansion - draw lines
- 22-05-26 Xi'an interview question (01) preparation
- 单调栈-42. 接雨水
- 【Rust 笔记】11-实用特型
- The method for win10 system to enter the control panel is as follows:
- [concurrent programming] concurrent tool class of thread
- ES6 promise learning notes
- TP5 order multi condition sort
猜你喜欢
注解简化配置与启动时加载
TP5 multi condition sorting
LeetCode 241. 为运算表达式设计优先级
单调栈-503. 下一个更大元素 II
Binary tree sorting (C language, int type)
Unity editor expansion - draw lines
Vscode connect to remote server
Redux - learning notes
Log4j2 vulnerability recurrence and analysis
Facial expression recognition based on pytorch convolution -- graduation project
随机推荐
我们有个共同的名字,XX工
[concurrent programming] thread foundation and sharing between threads
Downward compatibility and upward compatibility
Gif remove blank frame frame number adjustment
Introduction to the usage of getopts in shell
Binary tree sorting (C language, int type)
Drawing maze EasyX library with recursive backtracking method
The method of replacing the newline character '\n' of a file with a space in the shell
On the difference and connection between find and select in TP5 framework
22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令
Method of intercepting string in shell
Use of sort command in shell
状态压缩DP AcWing 291. 蒙德里安的梦想
C language file reading and writing
Binary tree sorting (C language, char type)
Analysis of Alibaba canal principle
单调栈-84. 柱状图中最大的矩形
Concurrent programming (III) detailed explanation of synchronized keyword
JS ternary operator - learning notes (with cases)
【Rust笔记】06-包和模块