标签:
输入N个数,M次查询。
每次查询给出一个数x。
要求:每次查询输出前x个数中第i小的数。(i为第i次查询)
你可以假设M <= N,Xi <= Xi+1 <= Xi+2 <= ……. <= Xm (Xm <= N).
1 7 4 3 1 -4 2 8 -1000 2 1 2 6 6
3 3 1 2
AC代码:
#include <stdio.h> #define inf 2000000000 int a[30005]; void quicksort(int left,int right); int main() { int T,M,N,i,j,k; scanf("%d",&T); while(T--) { scanf("%d%d",&N,&M); for(i=0;i<N;i++) scanf("%d",&a[i]); for(i=0;i<M;i++) { scanf("%d",&k); quicksort(0,k-1);//快排 printf("%d\n",a[i]);//第i小值 } } return 0; } void quicksort(int left,int right) { int i,j,t,temp; if( left > right ) { return; } i = left; j = right; t = a[left]; while( i != j ) { while( a[j]>= t && i<j ) { j--; } while( a[i]<= t && i<j ) { i++; } if(i < j) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } a[left] = a[i]; a[i] = t; quicksort(left,i-1); quicksort(i+1,right); return; }
优化代码下次更新..
标签:
原文地址:http://www.cnblogs.com/ldy-miss/p/5886814.html