求助!AC 60%
查看原帖
求助!AC 60%
601270
Secret_Sky_2021楼主2023/8/26 15:30
#include<iostream>
#include<algorithm>
#include<ctime>
using namespace std;
const int N=5000100;
int n,m,a[N];
int qs(int i,int j){
	srand(time(0));
	int key=a[(rand()%(j-i+1))+i];
	while(i<j){
		while(i<j&& a[j]>=key) j--;
		if(i<j) a[i]=a[j];
		while(i<j&& a[i]<=key) i++;
		if(i<j) a[j]=a[i];
	}
	a[i]=key;
	return i;
}
int find(int left, int right, int k)
{	
	int tem=qs(left,right);
	if(k==tem) printf("%d",a[k]);
	else if(k-1<tem) find(left,tem-1,k);
	else find(tem+1,right,k);
	return 0;
}
int main(){
	cin>>n>>m;
	for(int i=0;i<n;i++) scanf("%d",&a[i]);
	find(0,n-1,m);
	return 0;
}
/*
5 1
4 3 2 1 5
---------
2
*/
2023/8/26 15:30
加载中...