微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

【蓝桥杯】区间k大数查询

问题描述

给定一个序列,每次询问序列中第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 区间K大数查询 {

	public static int arr[];

	public static int result[];

	public static int l,k;

	public static int max;

	public static int temp[];

	public static void main(String[] args) {
		Scanner sc = new Scanner(system.in);
		int n = sc.nextInt();
		arr = new int[n];
		for (int i = 0; i < n; i++) {
			arr[i] = sc.nextInt();
		}
		int m = sc.nextInt();
		result = new int[m];
		for (int i = 0; i < m; i++) {
			l = sc.nextInt();
			r = sc.nextInt();
			k = sc.nextInt();
			calculate(l,k,i);
		}
		sc.close();
		for(int i=0; i<result.length; i++) {
			System.out.println(result[i]);
		}
	}

	private static void calculate(int l,int r,int k,int index) {
		temp = new int[r - l + 1];
		int t = 0;
		for (int i = l - 1; i <= r - 1; i++) {
			temp[t] = arr[i];
			t++;
		}
		Arrays.sort(temp);
		result[index] = temp[temp.length - k];
	}

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐