蓝桥杯-区间k大数查询(java)
生活随笔
收集整理的這篇文章主要介紹了
蓝桥杯-区间k大数查询(java)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
算法訓(xùn)練 區(qū)間k大數(shù)查詢 時間限制:1.0s 內(nèi)存限制:256.0MB問題描述給定一個序列,每次詢問序列中第l個數(shù)到第r個數(shù)中第K大的數(shù)是哪個。輸入格式第一行包含一個數(shù)n,表示序列長度。第二行包含n個正整數(shù),表示給定的序列。第三個包含一個正整數(shù)m,表示詢問個數(shù)。接下來m行,每行三個數(shù)l,r,K,表示詢問序列從左往右第l個數(shù)到第r個數(shù)中,從大往小第K大的數(shù)是哪個。序列元素從1開始標(biāo)號。輸出格式總共輸出m行,每行一個數(shù),表示詢問的答案。樣例輸入51 2 3 4 521 5 22 3 2樣例輸出42數(shù)據(jù)規(guī)模與約定對于30%的數(shù)據(jù),n,m<=100;對于100%的數(shù)據(jù),n,m<=1000;保證k<=(r-l+1),序列中的數(shù)<=106。
package com.sihai.advance;import java.util.Scanner;public class First {public static void main(String[] args) {int i, j, c, e = 0, u, max;int[] a = new int[1000];Scanner scanner = new Scanner(System.in);int count = scanner.nextInt();//獲取輸入數(shù)據(jù)int[] temp = new int[count];for(i = 0; i < count; i++){temp[i] = scanner.nextInt();}int m = scanner.nextInt();for(j = 0; j < m; j++){int l = scanner.nextInt();int r = scanner.nextInt();int k = scanner.nextInt();for (i = l - 1; i < r; i++) a[e++] = temp[i]; for (u = 1; u <= e - 1; u++) { for (c = 0; c <= e-u-1; c++) { if (a[c] < a[c + 1]) { int t = a[c]; a[c] = a[c + 1]; a[c + 1] = t; } } } max = a[k-1]; System.out.println(max);e = 0; }}// 對輸入的數(shù)據(jù)進(jìn)行由大到小排序public static int[] sort(int[] temp) {int t;for (int i = 0; i < temp.length - 1; i++) {for (int j = i + 1; j < temp.length; j++) {if (temp[i] < temp[j]) {t = temp[i];temp[i] = temp[j];temp[j] = t;}}}return temp;}}
總結(jié)
以上是生活随笔為你收集整理的蓝桥杯-区间k大数查询(java)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 信息系统项目管理知识--项目时间管理
- 下一篇: 蓝桥杯-K好数(java)