第X大的数
Time Limit: 1000MS
Memory Limit: 65536KB
Problem Description
X最近爱上了区间查询问题,给出N (N <= 100000) 个数,然后进行M (M <= 5) 次询问,每次询问时,输入一个数X (1 <= X <= N),输出N个数中第X大的数。
Input
多组输入。
每组首先输入一个整数N,代表有N个数,下面一行包含N个整数,用空格隔开。然后为一个整数M,代表有M次询问,下面的M行,每行一个整数X。
Output
输出N个数中第X大的数。
Example Input
4 1 2 2 3 4 1 2 3 4
Example Output
3 2 2 1
Hint
Author
zmx
参考代码
#include<stdio.h>
int a[100000];
void sort(int l,int r)
{
int key = a[l];
int i = l, j = r;
while( i < j )
{
while( i < j && key >= a[j] ) j--;
a[i] = a[j];
while( i < j && key <= a[i] ) i++;
a[j] = a[i];
}
a[i] = key;
if( l < r )
{
sort(l,i-1);
sort(i+1,r);
}
}
int main()
{
int n,i,m;
int x;
while(~scanf("%d",&n))
{
for( i = 0; i < n; i++ )
{
scanf("%d",a+i);
}
sort(0,n-1);
scanf("%d",&m);
while(m--)
{
scanf("%d",&x);
printf("%d\n",a[x-1]);
}
}
return 0;
}