Directory
1 Problem Description
2 Solutions
1 problem description Problem Description
Given a sequence, the number of the number L to the r number in each query sequence is the First.
Input Format
The first row contains a number n, which represents the sequence Length.
The second row contains n positive integers that represent the given sequence.
The third consists of a positive integer m, which indicates the number of Queries.
The next m line, three numbers per line l,r,k, indicates the question sequence from left to right the number of R number, from the big to the small K large number is Which. Sequence elements are labeled starting from 1.
output Formatoutputs a total of M rows, one number per line, indicating the answer to the Question. Sample Input5
1 2 3) 4 5
2
1 5 2
2 3 2Sample Output4
2data size and conventions
For 30% of data,n,m<=100;
For 100% of data,n,m<=1000;
Guaranteed k<= (r-l+1), number <=106 in the Sequence.
2 Solutions
This topic mainly examines the sorting, considering the time efficiency and the stability, this question chooses the merge sort Best.
The specific code is as Follows:
Importjava.util.Scanner; public classMain {//the elements in the array, start to end, are merged, and the interval is sorted in descending order. public voidMergeSort (int[] array,intStartintEnd) { if(end-start >= 1) { int[] Leftarray = Gethalfarray (array, start, end, 0); int[] Rightarray = Gethalfarray (array, start, end, 1); MergeSort (leftarray,0, leftarray.length-1); MergeSort (rightarray,0, rightarray.length-1); Getmerge (array, start, leftarray, rightarray); } } //gets the half element of the array start~end according to Judge public int[] Gethalfarray (int[] array,intStartintEndintJudge) { int[] half; intLen = End-start + 1; if(judge = = 0) { intLength = LEN/2; Half=New int[length]; for(inti = 0;i < length;i++) half[i]= Array[start +i]; } Else { intLength = LEN-LEN/2; Half=New int[length]; for(inti = 0;i < length;i++) {half[i]= Array[start + LEN/2 +i]; } } returnhalf; } //merges the left and right half elements of an array of arrays, returning descending order public voidGetmerge (int[] array,intStartint[] leftarray,int[] Rightarray) { inti = 0, J = 0; while(i < Leftarray.length && J <Rightarray.length) {if(leftarray[i] >=rightarray[j]) Array[start+ +] = leftarray[i++]; ElseArray[start+ +] = rightarray[j++]; } while(i < Leftarray.length) array[start++] = leftarray[i++]; while(j < Rightarray.length) array[start++] = rightarray[j++]; } public voidPrintresult (int[] array,int[] [] Query) { int[] result =New int[query.length]; for(inti = 0;i < query.length;i++) { int[] Temparray =New int[array.length];//this gets the cloned object of the array, requiring the address to be Changed. If you assign a value directly, the two addresses are the same for(intj = 0;j < array.length;j++) temparray[j]=array[j]; intStart = Query[i][0]; intEnd = Query[i][1]; intK = query[i][2]; if(k < 0 | | k > end-start + 1)//prevent k out of bounds Continue; MergeSort (temparray, Start-1, end-1); result[i]= Temparray[start-1 + k-1]; } //Output Results for(inti = 0;i < result.length;i++) System.out.println (result[i]); } public Static voidmain (string[] Args) {main test=NewMain (); Scanner in=NewScanner (system.in); intn =In.nextint (); int[] array =New int[n]; for(inti = 0;i < array.length;i++) array[i]=In.nextint (); intm =In.nextint (); if(n > | | m > 1000) return; int[] query =New int[m] [3]; for(inti = 0;i < m;i++) {query[i][0] =In.nextint (); query[i][1] =In.nextint (); query[i][2] =In.nextint (); } test.printresult (array, query); }}
Algorithm note _079: Blue Bridge cup practice interval k large number query (Java)