HDOJ 1280查找前k大的数字

Problem Description
还记得Gardon给小希布置的那个作业么?(上次比赛的1005)其实小希已经找回了原来的那张数表,现在她想确认一下她的答案是否正确,但是整个的答案是很庞大的表,小希只想让你把答案中最大的M个数告诉她就可以了。 
给定一个包含N(N<=3000)个正整数的序列,每个数不超过5000,对它们两两相加得到的N*(N-1)/2个和,求出其中前M大的数(M<=1000)并按从大到小的顺序排列。
 

Input
输入可能包含多组数据,其中每组数据包括两行: 
第一行两个数N和M, 
第二行N个数,表示该序列。

 

Output
对于输入的每组数据,输出M个数,表示结果。输出应当按照从大到小的顺序排列。
 

Sample Input
4 4
1 2 3 4


Sample Output
7 6 5 5

 

这就是题目,开始我利用快速排序找第K大的元素,然后将前面的元素再快速排序,以为这样会快。。。感觉直接排序会慢很多。。。代码是:
import java.util.Arrays;
import java.util.Scanner;

public class HDOJ1280 {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);
		
		
		while(scan.hasNext()){
			int n = scan.nextInt();
			int k = scan.nextInt();
			int a[] = new int [n+1];
			int b[] = new int [k+1];
			int x = n*(n-1)/2;
			int c [] = new int [x+1];
			for(int i = 1;i<=n;i++){
				a[i] = scan.nextInt();
			}
			int location = 1;
			for(int i = 1 ; i<=n;i++){
				for(int j = i+1 ; j<=n;j++){
					c[location] = a[i] + a[j];	
					location++;
				}
			}

			new HDOJ1280().findKth(c, b, k, 1, x);
			new HDOJ1280().quick(c, 1, k);
			
			for(int i = 1 ; i<= k  ; i ++){
				System.out.print(c[i]+" ");
			}
			System.out.println();
			
		}


		

	}

	public int partition(int a[], int f, int r) {

		int point = f;
		int i = f + 1;
		int j = r;
		int p = a[point];
		while (true) {
			while (i <=r && a[i] > p) {
				i++;
			}
			while (j > 0 && a[j] < p) {
				j--;
			}

			if (j <= i) {
				break;
			} else {
				int x = a[j];
				a[j] = a[i];
				a[i] = x;
			}
		}

		a[point] = a[j];
		a[j] = p;

		return j;

	}
	
	public void quick(int a[] , int f, int r){//快速排序
		if(f<r){
			int p = partition(a,f,r);
			quick(a,f,p-1);
			quick(a,p+1,r);
		}
	}

	public void findKth(int a[], int b[], int k, int f, int r) {//查找第K大的元素后返回,就是前面是前K大元素

		if (f < r) {
			int p = partition(a, f, r);
			if (p == k) {
				return ;
			} else {
				if (p < k) {
					findKth(a, b, k, p + 1, r);
				} else {
					findKth(a, b, k, f, p - 1);
				}

			}

		}
		return ;
	}
}
但是提交后 Time Limit Exceeded, 后来改用Java里面Arrays.sort,通过了,而且代码仅用了1/3,
import java.util.Arrays;
import java.util.Scanner;

public class test {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);

		while (scan.hasNext()) {
			int n = scan.nextInt();
			int k = scan.nextInt();
			int a[] = new int[n];
			int x = n * (n - 1) / 2;
			int c[] = new int[x];
			for (int i = 0; i < n; i++) {
				a[i] = scan.nextInt();
			}
			int location = 0;
			for (int i = 0; i <n; i++) {
				for (int j = i + 1; j < n; j++) {
					c[location] = a[i] + a[j];
					location++;
				}
			}
			Arrays.sort(c);
			for (int i = x-1; i>x-1-k; i--) {
				System.out.print(c[i]);
				if(i!=x-k)
					System.out.print(" ");
			}
			System.out.println();
		}
	}
}

感觉奇怪,看了Java API才发现,原来sort方法就是利用快速排序的。应该是数据<=3000,我的算法不能体现优点,当数据非常多时,感觉我的就会比Arrays.sort快了。紧紧是感觉,我也不能输入几千个数来测试。。,
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值