算法训练 区间k大数查询 Java 解题

>>算法训练 区间k大数查询  

时间限制:1.0s   内存限制:256.0MB

问题描述

给定一个序列,每次询问序列中第l个数到第r个数中第K大的数是哪个。

输入格式

第一行包含一个数n,表示序列长度。

第二行包含n个正整数,表示给定的序列。

第三个包含一个正整数m,表示询问个数。

接下来m行,每行三个数l,r,K,表示询问序列从左往右第l个数到第r个数中,从大往小第K大的数是哪个。序列元素从1开始标号。

输出格式
总共输出m行,每行一个数,表示询问的答案。
样例输入
5
1 2 3 4 5
2
1 5 2
2 3 2
样例输出
4
2
数据规模与约定

对于30%的数据,n,m<=100;

对于100%的数据,n,m<=1000;

保证k<=(r-l+1),序列中的数<=106

 1 import java.util.Arrays;
 2 import java.util.Scanner;
 3 
 4 public class Algo_01_MaxSel {
 5     public static void main(String[] args) {
 6         int n, m, l, r, k;
 7         int array[], result[], temp[];
 8         Scanner scanner = new Scanner(System.in);
 9         
10         // ***************n是序列长度
11         n = scanner.nextInt();
12         
13         array = new int[n];
14 
15         // ***************输入序列
16         for (int i = 0; i < n; i++) {
17             array[i] = scanner.nextInt();
18         }
19 
20         // ***************输入查询次数
21         m = scanner.nextInt();
22         result = new int[m];
23 
24         // ***************输入每次查询的要求,左起->右至,第k大
25         for (int i = 0; i < m; i++) {
26 
27             l = scanner.nextInt();
28             r = scanner.nextInt();
29             k = scanner.nextInt();
30 
31             // 定义暂存数组,他的个数与区间个数相等
32             int temp_i = 0;
33             int length = r - l + 1;
34             temp = new int[length];
35 
36             // 将区间数字一个个赋值到暂存数组里
37             for (int j = (l - 1); j < (r - 1); j++) {
38                 temp[temp_i] = array[j];
39                 temp_i++;
40             }
41 
42             // 对暂存数组进行排序,结果三从小到大的
43             Arrays.sort(temp);
44             // 将第k大的数字复制给结果数组
45             result[i] = temp[length - k + 1];
46         }
47 
48         // ***************分成 m 行输出查询结果
49         for (int i = 0; i < m; i++) {
50             System.out.println(result[i]);
51         }
52 
53     }
54 }

 我自己的代码未通过检测,以下是网友的解题

http://blog.csdn.net/u011506951/article/details/26820363

 1 import java.util.Arrays;  
 2 import java.util.Scanner;  
 3   
 4 public class Algo_01_MaxSel {  
 5   
 6     public static void main(String[] args) {  
 7         Scanner sc = new Scanner(System.in);  
 8         int n = sc.nextInt();  
 9         
10         //定义一个数组num用于存放数列
11         int[] num = new int[n];  
12         sc.nextLine();  
13         
14         //输入数列
15         for (int i = 0; i < n; i++) {  
16             num[i] = sc.nextInt();  
17         }  
18   
19         //输入查询的次数
20         int m = sc.nextInt();  
21   
22         //定义存放查询指令的二维数组arr
23         int[][] arr = new int[m][3];
24 
25         //输入查询的指令
26         for (int i = 0; i < m; i++) {  
27             arr[i][0] = sc.nextInt();  
28             arr[i][1] = sc.nextInt();  
29             arr[i][2] = sc.nextInt();  
30         }  
31   
32         
33         for (int i = 0; i < m; i++) {  
34             //定义暂存数组,区间就是二维数组里,每一行的第2个数减去第1个数+1
35             int[] temp = new int[arr[i][1] - arr[i][0] + 1];  
36             //复制区间里的数字,注意数列是从1开始,而数组下标是0开始,所以,查询条件左右都需-1
37             //temp = Arrays.copyOfRange(num, arr[i][0] - 1, arr[i][1]原作者此处忘了-1);  
38             temp = Arrays.copyOfRange(num, arr[i][0] - 1, arr[i][1]-1);  
39             Arrays.sort(temp);  
40   
41             System.out.println(temp[arr[i][1] - arr[i][0] + 1 - arr[i][2]]);  
42         }  
43     }  
44 }
原文地址:https://www.cnblogs.com/jentle/p/3757400.html