1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| #include<cmath> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std;
int n,m,k; int a[1100000];
int ChooseSort(int left,int right); int BFPRT(int left,int right,int k); int GetPivotIndex(int left,int right); int Partition(int left,int right,int pivot_index);
int ChooseSort(int left,int right)
{ int temp; for(int i=left;i<right;i++) for(int j=i+1;j<=right;j++) { if(a[i]>a[j])swap(a[i],a[j]); } return(((right-left)/2)+left); }
int GetPivotIndex(int left,int right) { if (right-left<5)return(ChooseSort(left, right));
int sub_right=left-1; for (int i=left;i+4<=right;i+=5) { int index=ChooseSort(i,i+4); swap(a[++sub_right], a[index]); }
return BFPRT(left,sub_right,((sub_right-left+1)/2)+1); }
int Partition(int left,int right,int pivot_index)
{ swap(a[pivot_index],a[right]);
int divide_index=left; for (int i=left;i<right;i++) { if (a[i]<a[right]) swap(a[divide_index++],a[i]); }
swap(a[divide_index], a[right]); return divide_index; }
int BFPRT(int left,int right,int k)
{ int pivot_index=GetPivotIndex(left,right); int divide_index = Partition(left,right,pivot_index); int num=divide_index-left+1; if (num == k)return divide_index; else if (num>k)return BFPRT(left,divide_index-1,k); else return BFPRT(divide_index+1,right,k-num); }
int main() { scanf("%d%d",&n,&k); for(int i=1;i<=n;i++)scanf("%d",&a[i]); printf("%d",a[BFPRT(1,n,n-k+1)]); return 0; }
|