给定一个序列,每次询问序列中第l个数到第r个数中第K大的数是哪个。
第一行包含一个数n,表示序列长度。
第二行包含n个正整数,表示给定的序列。
第三个包含一个正整数m,表示询问个数。
接下来m行,每行三个数l,r,K,表示询问序列从左往右第l个数到第r个数中,从大往小第K大的数是哪个。序列元素从1开始标号。
1 2 3 4 5
2
1 5 2
2 3 2
2
对于30%的数据,n,m<=100;
对于100%的数据,n,m<=1000;
保证k<=(r-l+1),序列中的数<=106。
#include<stdio.h>
int main()
{
void max(int a[],int l,int r,int k);
int n,m,i,j,l[1000],r[1000],k[1000];
scanf("%d",&n);
int arr[n];
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
scanf("%d",&m);
for(j=0;j<m;j++)
{
scanf("%d %d %d",&l[j],&r[j],&k[j]);
}
for(j=0;j<m;j++)
max(arr,l[j],r[j],k[j]);
}
void max(int a[],int l,int r,int k)
{
int cout,b,c,d,temp[r-l+2],swap;
for(cout=l,b=0;cout<=r;cout++,b++)
{
temp[b]=a[cout-1];
}
for(c=0;c<b;c++)
{
for(d=c+1;d<b;d++)
if(temp[c]<temp[d])
{
swap=temp[c];
temp[c]=temp[d];
temp[d]=swap;
}
}
printf("%d\n",temp[k-1]);
}