Given a sequence, each time it is asked which is the Kth largest number among the lth to rth numbers in the sequence.
The first line contains a number n representing the sequence length.
The second line contains n positive integers representing the given sequence.
The third one contains a positive integer m, indicating the number of queries.
The next m lines, each with three numbers l, r, and K, indicate which of the lth to rth numbers in the query sequence from left to right, which is the Kth largest number from large to small. Sequence elements are numbered starting from 1.
1 2 3 4 5
2
1 5 2
2 3 2
2
For 30% of the data, n,m<=100;
For 100% of the data, n,m<=1000;
It is guaranteed that k<=(r-l+1) and the number in the sequence <=10 6 .
Thought analysis:
First, it is necessary to sort the corresponding interval of the input data, in order to find the ranking corresponding to each number in the corresponding interval every time, and the corresponding interval K value can be output.
The first is the sorting algorithm. There are many sorting algorithms. Since the data in this question is relatively simple, the simple quick sorting method is used. The following is the C++ code of the simple sorting algorithm:
void Swap(int &a,int &b){//Use references to exchange two numbers int temp=0; temp=a; a=b; b=temp; } void sortj(int a[],int n){//sorting algorithm descending int i,j,t,temp; for(i=0;i<n;i++){ t=i; for(j=i;j<n;j++){ if(a[t]<a[j]) t=j; } Swap(a[t],a[i]); } }
Then we use the function that corresponds to the array copy of each interval. Through this function, each interval array is confirmed once to facilitate future sorting and output; the code is as follows:
void copyArr(int a[],int b[],int begin,int end){ int i,j=0; for(i=begin-1;i<=end-1;i++){ b[j]=a[i]; j++; } }
Finally, the overall code of the entire program is given to you for reference only. Since I am a student in school, the normative code is very lacking. I hope you guys don't mind! If you don't understand, leave a message below, I will reply in time;
#include <iostream> using namespace std; /* *Program function: input a set of data, and then input a set of two-dimensional data * Find the Kth largest element in the middle of lr and output */ void Swap(int &a,int &b){//Use references to exchange two numbers int temp=0; temp=a; a=b; b=temp; } void sorts(int a[],int n){//Sort algorithm ascending int i,j,t,temp; for(i=0;i<n;i++){ t=i; for(j=i;j<n;j++){ if(a[t]>a[j]) t=j; } Swap(a[t],a[i]); } } void sortj(int a[],int n){//sorting algorithm descending int i,j,t,temp; for(i=0;i<n;i++){ t=i; for(j=i;j<n;j++){ if(a[t]<a[j]) t=j; } Swap(a[t],a[i]); } } //Copy the progress of each interval to another array void copyArr(int a[],int b[],int begin,int end){ int i,j=0; for(i=begin-1;i<=end-1;i++){ b[j]=a[i]; j++; } } int main(){ int n,arr[100000],m; int a[100000][3];//Query two-dimensional array int ar[100000];//Backup copy array cin>>n; for(int i=0;i<n;i++){ cin>>arr[i]; } cin>>m;//Number of times to query for(int i=0;i<m;i++){//The input sequence of each query is stored in a two-dimensional array for(int j=0;j<3;j++){ cin>>a[i][j]; } } for(int i=0;i<m;i++){ copyArr(arr,ar,a[i][0],a[i][1]); sortj(ar,a[i][1]-a[i][0]+1); cout<<ar[a[i][2]-1]<<endl; } return 0; }