59 区间k大数查询

59 区间k大数查询

作者: Turbo时间限制: 1S章节: 基本练习(数组)

问题描述 :

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

注意,由于存在相等的元素,因此,第2大的数可能和第1大的数相等。

输入说明 :

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

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

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

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

n,m<=1000;

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

输出说明 :

总共输出m行,每行一个数,表示询问的答案。

输入范例 :

5
1 5 3 4 5
2
1 5 2
2 3 2

输出范例 :

5
3

import java.util.*;

public class test_59 {
    /**
     * 59 区间k大数查询
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.nextLine();
        String[] split = sc.nextLine().trim().split(" ");
        ArrayList<Integer> lists = new ArrayList<>();
        for (int i = 0; i < split.length; i++) {
            lists.add(Integer.valueOf(split[i]));
        }
        ArrayList<Integer> subLists = new ArrayList<>();
        int m = sc.nextInt();
        sc.nextLine();
        String[] split1;
        int start,end,k;
        while(m-->0){
            split1 = sc.nextLine().trim().split(" ");
            start = Integer.valueOf(split1[0]);
            end = Integer.valueOf(split1[1]);
            k = Integer.valueOf(split1[2]);
            for (int i = start-1; i < end; i++) {
                subLists.add(lists.get(i));
            }
            Collections.sort(subLists, new Comparator<Integer>() {
                @Override
                public int compare(Integer o1, Integer o2) {
                    return o2-o1;
                }
            });
            System.out.println(subLists.get(k-1));
            Iterator<Integer> iterator = subLists.iterator();
            while (iterator.hasNext()){
                iterator.next();
                iterator.remove();
            }
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值