这题我在网上看说因为Java的原因 即使尽可能优化 也难以AC 就60 但我没用那个人的算法 就排序用Arrays.sort(),快速输入(StreamTokenizer)也是60....
后来使用快速排序代替Arrays.sort() 但还是T了,之后我在AC的代码中找了一个人的
如下:(最后有完整代码)
static int maxIndex;
public static void quickSort(int[] arr,int left,int right){
if (left < right){
swap(arr,left+(int)(Math.random() * (right-left+1)),right);
int[] p = partition(arr,left,right);
if (maxIndex < p[0]){
quickSort(arr,left,p[0]-1);
}else if (maxIndex > p[0]){
quickSort(arr,p[1] + 1,right);
}
/*quickSort(arr,p[1] + 1,right);
quickSort(arr,left,p[0] - 1);*/
}
}
//这是一个处理arr[l...r]的函数
//默认以arr[l]作划分,arr[r] ->p <p ==p >p
//返回等于区域(左边界,右边界),所以返回一个长度为2的数组res, res[0] res[1]
private static int[] partition(int[] arr, int L, int R) {
int less = L - 1;//<区右边界
int more = R;//>区左边界(不用管最后一个数,最后要换中间的)
while (L < more){//L表示当前数的位置 arr[R] -->划分值
if (arr[L] < arr[R]){//当前数 < 划分值
swap(arr,++less,L++);
}else if (arr[L] > arr[R]){//当前数 > 划分值
swap(arr,--more,L);
}else {
L++;
}
}
swap(arr,more,R);
return new int[]{less + 1,more};
}
private static void swap(int[] arr, int i, int l) {
int temp = arr[i];
arr[i] = arr[l];
arr[l] = temp;
}
这是完整代码:(请忽略QuickSort())
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class P1923求第k小的数 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
st.nextToken();
int n = (int) st.nval;
st.nextToken();
maxIndex = (int) st.nval;
int[] a = new int[n];
for (int i = 0; i < a.length; i++) {
st.nextToken();
a[i] = (int) st.nval;
}
// Arrays.sort(a);
quickSort(a, 0, a.length-1);
pr.print(a[maxIndex]);
pr.flush();
}
static int maxIndex;
public static void quickSort(int[] arr,int left,int right){
if (left < right){
swap(arr,left+(int)(Math.random() * (right-left+1)),right);
int[] p = partition(arr,left,right);
if (maxIndex < p[0]){
quickSort(arr,left,p[0]-1);
}else if (maxIndex > p[0]){
quickSort(arr,p[1] + 1,right);
}
/*quickSort(arr,p[1] + 1,right);
quickSort(arr,left,p[0] - 1);*/
}
}
//这是一个处理arr[l...r]的函数
//默认以arr[l]作划分,arr[r] ->p <p ==p >p
//返回等于区域(左边界,右边界),所以返回一个长度为2的数组res, res[0] res[1]
private static int[] partition(int[] arr, int L, int R) {
int less = L - 1;//<区右边界
int more = R;//>区左边界(不用管最后一个数,最后要换中间的)
while (L < more){//L表示当前数的位置 arr[R] -->划分值
if (arr[L] < arr[R]){//当前数 < 划分值
swap(arr,++less,L++);
}else if (arr[L] > arr[R]){//当前数 > 划分值
swap(arr,--more,L);
}else {
L++;
}
}
swap(arr,more,R);
return new int[]{less + 1,more};
}
private static void swap(int[] arr, int i, int l) {
int temp = arr[i];
arr[i] = arr[l];
arr[l] = temp;
}
// 快排
static int qsum;
static void QuickSort(int array[], int low, int high) {
int i = low;
int j = high;
int x;
if (i > j)
return;
int temp = array[low];
while (i != j) {
while (array[j] >= temp && i < j)
j--;
while (array[i] <= temp && i < j)
i++;
if (i < j) {
qsum += 1;
x = array[i];
array[i] = array[j];
array[j] = x;
}
}
// 将基准temp放于自己的位置,(第i个位置)
x = array[low];
array[low] = array[i];
array[i] = x;
qsum += 1;
QuickSort(array, low, i - 1);
QuickSort(array, i + 1, high);
}
}
文章讨论了一段Java代码,该代码使用快速排序算法尝试解决在线编程挑战中的一个问题。尽管使用了Arrays.sort()和StreamTokenizer进行输入处理,但代码最初未能通过所有测试用例。作者随后尝试了不同的快速排序实现,但仍然遇到问题。最终,作者分享了一段经过优化的快速排序代码,该代码能够正确找到第k小的数并成功通过了测试。
510

被折叠的 条评论
为什么被折叠?



