资源限制
时间限制: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。
Code
Java源代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); // 此类用于控制台读取数据
int n = sc.nextInt(); // 调用方法在控制台读取一个int的整数
int[] a = new int[n];
for (int i = 0; i < n; i++) { // 输入
a[i] = sc.nextInt();
}
int m = sc.nextInt();
for (int i = 0; i < m; i++) {
int l = sc.nextInt();
int r = sc.nextInt();
int k = sc.nextInt();
int[] b = new int[r - l + 1]; // 创建一个数组来保存l到r的值
for (int j = 0; j < b.length; j++) {// 获取区间值
b[j] = a[l - 1];
l++;
}
for (int j = 0; j < b.length; j++) {// 冒泡排序,从大到小
for (int p = j + 1; p < b.length; p++) {
if (b[j] < b[p]) {
int tmp = b[j];
b[j] = b[p];
b[p] = tmp;
}
}
}
System.out.println(b[k - 1]); // 直接输出第k个值
}
}
}
下面的代码框存在错误经过多次修改修改后的正确代码框在第三框中:
错误:
输入:
10
9759 24222 18589 6423 24947 27507 13031 16414 29169 901
92
3 6 3
5 10 6
4 9 2
2 3 1
2 7 3
1 7 5
9 10 1
2 5 3
4 10 1
1 9 9
8 8 1
4 9 2
2 8 3
4 8 5
7 10 4
1 2 2
7 10 3
4 5 1
5 10 2
3 6 2
4 4 1
5 8 4
1 9 4
1 9 6
2 9 6
9 10 2
3 3 1
3 9 2
1 8 7
2 6 4
2 7 3
3 5 1
7 9 2
6 7 2
3 10 5
2 7 1
2 3 2
8 10 1
3 10 4
1 3 3
4 10 5
2 9 6
4 6 2
3 6 2
7 9 1
3 8 3
5 10 6
7 10 2
3 9 6
5 6 2
2 3 2
1 9 1
4 10 5
7 8 2
8 10 1
4 10 5
7 8 1
6 9 3
5 6 1
2 7 2
4 7 2
6 6 1
3 9 6
4 6 1
2 9 7
2 3 1
3 7 5
2 6 1
4 7 1
3 10 8
4 5 1
1 6 4
4 7 3
3 9 5
6 8 1
2 3 2
5 8 3
5 5 1
3 10 2
1 8 2
8 9 2
1 4 1
3 6 3
1 4 4
3 10 8
4 6 2
3 4 2
2 4 1
4 6 1
2 3 1
3 7 5
1 8 1
一直报错下标越界
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] a = new int[n + 1];
int[] c = new int[n + 1];
int cn = 0;
for (int i = 1; i <= n; i++) {
a[i] = scan.nextInt();
}
int m = scan.nextInt();
for (int i = 0; i < m; i++) {
int l = scan.nextInt();
int r = scan.nextInt();
int K = scan.nextInt();
int[] b = new int[r - l + 1]; // r-l+1表示存储l到r的共r-l+1个数
int num = 0;
for (int j = l; j <= r; j++) {
b[num++] = a[j];
}
Arrays.sort(b); //实现的是从小到大排序
c[cn++] = b[r - l + 1 - K]; //因为b数组中元素是按从小到大排序,题目要求从大到小所以用r - l + 1 - K(r-l+1表示共有几个数排序再减去要输入第K个数就实现了输出从大到小的第K个数的原理)
}
for (int i = 0; i < m; i++) {
System.out.println(c[i]);
}
}
}
订正第二段代码框中的错误:解决方法直接输出b数组找到的第K个数不用再存入c数组
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] a = new int[n + 1];
int[] c = new int[n + 1];
int cn = 0;
for (int i = 1; i <= n; i++) {
a[i] = scan.nextInt();
}
int m = scan.nextInt();
for (int i = 0; i < m; i++) {
int l = scan.nextInt();
int r = scan.nextInt();
int K = scan.nextInt();
int[] b = new int[r - l + 1]; // r-l+1表示存储l到r的共r-l+1个数
int num = 0;
for (int j = l; j <= r; j++) {
b[num++] = a[j];
}
Arrays.sort(b); //实现的是从小到大排序
System.out.println(b[r - l + 1 - K]); //因为b数组中元素是按从小到大排序,题目要求从大到小所以用r - l + 1 - K(r-l+1表示共有几个数排序再减去要输入第K个数就实现了输出从大到小的第K个数的原理)
}
}
}