当前位置:网站首页>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 笔记】12-闭包
- 22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
- Alibaba canaladmin deployment and canal cluster Ha Construction
- 记忆化搜索 AcWing 901. 滑雪
- Allocation exception Servlet
- 如何应对数仓资源不足导致的核心任务延迟
- Markdown learning
- too many open files解决方案
- [concurrent programming] collaboration between threads
- PIC16F648A-E/SS PIC16 8位 微控制器,7KB(4Kx14)
猜你喜欢

Allocation exception Servlet

Deeply understand the underlying data structure of MySQL index

Dom4j traverses and updates XML

XPath实现XML文档的查询

Really explain the five data structures of redis

Unity editor expansion - draw lines

On the setting of global variable position in C language

Gaussian elimination acwing 883 Gauss elimination for solving linear equations

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

Gif remove blank frame frame number adjustment
随机推荐
单调栈-84. 柱状图中最大的矩形
LeetCode 75. 颜色分类
[concurrent programming] concurrent security
Parameters of convolutional neural network
Deep parsing JVM memory model
数位统计DP AcWing 338. 计数问题
请求参数的发送和接收
Dom4j traverses and updates XML
22-05-26 西安 面试题(01)准备
【Rust 笔记】10-操作符重载
[RPC] RPC remote procedure call
Concurrent programming (III) detailed explanation of synchronized keyword
Using variables in sed command
MySQL three logs
LeetCode 535. TinyURL 的加密与解密
Slice and index of array with data type
Alibaba canal actual combat
PHP uses foreach to get a value in a two-dimensional associative array (with instances)
The method for win10 system to enter the control panel is as follows:
[concurrent programming] working mechanism and type of thread pool