问题描述:
给定一个序列,每次询问序列中第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。
代码:
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int m = sc.nextInt();
int[] result = new int[m];
for (int i = 0; i < m; i++) {
int l = sc.nextInt();
int r = sc.nextInt();
int k = sc.nextInt();
int[] assist = new int[r - l + 1];
int index1 = 0;
for (int j = l - 1; j < r; j++) {
assist[index1++] = arr[j];
}
Arrays.sort(assist);
result[i] = assist[assist.length - k];
}
for (int i : result) {
System.out.println(i);
}
}
}
官方给出的输入文档:
42
18468 6335 26501 19170 15725 11479 29359 26963 24465 5706 28146 23282 16828 9962 492 2996 11943 4828 5437 32392 14605 3903 154 293 12383 17422 18717 19719 19896 5448 21727 14772 11539 1870 19913 25668 26300 17036 9895 28704 23812 31323
34
3 34 6
26 30 4
9 12 3
4 40 21
30 40 8
11 23 7
15 37 13
11 35 18
11 29 11
15 34 10
11 21 3
22 34 13
16 24 1
5 25 18
11 27 9
17 19 1
15 26 6
33 35 1
36 40 4
20 36 11
19 25 6
6 39 15
18 24 3
5 13 7
24 34 5
5 7 1
6 31 22
10 30 14
41 42 1
16 17 1
23 29 7
32 36 3
2 24 16
10 36 27
输出文档:
24465
17422
23282
14605
11539
9962
11943
5437
11943
11943
23282
154
32392
2996
11943
11943
5437
19913
17036
12383
293
17036
5437
15725
17422
29359
3903
5448
31323
11943
154
14772
5706
154