hdu 2523SORT AGAIN

</pre></h1><h1 style="color: rgb(26, 92, 200); text-align: center; font-family: 'Times New Roman';">SORT AGAIN</h1><span style="font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center;"><strong><span style="font-family: Arial; font-size: 12px; color: green;">Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4910    Accepted Submission(s): 1561</span></strong></span><span style="font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center;"><strong><span style="font-family: Arial; font-size: 12px; color: green;"></span></strong></span><br style="font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center;" /><br style="font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center;" /><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-color: transparent; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat;">Problem Description</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); margin: 0px; padding: 0px 20px; font-size: 14px; font-family: 'Times New Roman'; background-repeat: no-repeat repeat;">给你N个整数,x1,x2...xn,任取两个整数组合得到|xi-xj|,(0<i,j<=N,i!=j)。现在请你计算第K大的组合数是哪个(一个组合数为第K大是指有K-1个不同的组合数小于它)。</div><div class="panel_bottom" style="height: auto; margin: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center; background-position: 0% 0%; background-repeat: no-repeat no-repeat;"> </div><br style="font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center;" /><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-color: transparent; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat;">Input</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); margin: 0px; padding: 0px 20px; font-size: 14px; font-family: 'Times New Roman'; background-repeat: no-repeat repeat;">输入数据首先包含一个正整数C,表示包含C组测试用例.每组测试数据的第一行包含两个整数N,K。(1<N<=1000,0<K<=2000)接下去一行包含N个整数,代表x1,x2..xn。(0<=xi<=2000)</div><div class="panel_bottom" style="height: auto; margin: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center; background-position: 0% 0%; background-repeat: no-repeat no-repeat;"> </div><br style="font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center;" /><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-color: transparent; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat;">Output</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); margin: 0px; padding: 0px 20px; font-size: 14px; font-family: 'Times New Roman'; background-repeat: no-repeat repeat;">对于每组测试数据,请输出第K大的组合数,每个输出实例占一行。</div><div class="panel_bottom" style="height: auto; margin: 0px; background-image: url(http://acm.hdu.edu.cn/images/panel-bottom.png); font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center; background-position: 0% 0%; background-repeat: no-repeat no-repeat;"> </div><br style="font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center;" /><div class="panel_title" align="left" style="height: 38px; background-image: url(http://acm.hdu.edu.cn/images/panel-title.png); background-color: transparent; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background-position: 0% 100%; background-repeat: no-repeat no-repeat;">Sample Input</div><div class="panel_content" style="height: auto; background-image: url(http://acm.hdu.edu.cn/images/panel-content.png); margin: 0px; padding: 0px 20px; font-size: 14px; font-family: 'Times New Roman'; background-repeat: no-repeat repeat;"><pre style="word-wrap: break-word; white-space: pre-wrap; margin-top: 0px; margin-bottom: 0px;"><div style="font-family: 'Courier New', Courier, monospace;">3
3 2
4 0 7
4 2
1 2 3 4
2 1
2 9</div>
 

Sample Output
   
   
4 2 7
 
题意:给出n个数,计算从每个数与(n-1)的绝对值,并小到大第k大的绝对值数。用hash思想,把每组的绝对值的结果存在result中。
package hdu;

import java.util.Scanner;

public class p2523 {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int c=sc.nextInt();
		while(c-->0){
			int n=sc.nextInt();
			int k=sc.nextInt();
			int x[]=new int[n];
			for(int i=0;i<n;i++){
				x[i]=sc.nextInt();
			}
			int result[]=new int[2001];//hash思想,result用来存每个组合数的绝对值。
			//计算组合数,并hash一下
			for(int i=0;i<n;i++){
				for(int j=0;j<n;j++){
					if(i!=j){
						result[Math.abs(x[i]-x[j])]=1;//把存在的一对组合数设为1
					}
				}
			}
			int count=0;
			//要取第K大,即result值加起来等于k
			for(int i=0;i<result.length;i++){
				count+=result[i];
				if(count==k){
					System.out.println(i);
					break;
				}
			}
		}
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值