Dgucheng @ 2021-07-15 15:46:53
#include<bits/stdc++.h>
using namespace std;
int ans=0,k,n,a[5000001];
inline int read(){
int x=0,f=1;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-')f=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
x=(x<<1)+(x<<3)+(c^48);
c=getchar();
}
return x*f;
}
void findk(int l,int r){
if (l==r){
ans=a[l];
return ;
}
int i=l;
int j=r;
int f=a[(1+r)/2];
int t;
do {
while (a[i]<f)i++;
while (a[j]>f)j--;
if (i<=j){
t=a[i];
a[i]=a[j];
a[j]=t;
i++;
j--;
}
}while(i<=j);
if (k<=j)
findk(l,j);
else if (i<=k)
findk(i,r);
else findk(j+1,i-1);
}
int main(){
n=read();
k=read();
for(register int i=0;i<n;++i) a[i]=read();
findk(0,n-1);
printf("%d",ans);
return 0;
}```
by zhouyixian @ 2021-07-15 15:48:46
快排能过才见鬼了吧。。。
by lyhang @ 2021-07-15 15:51:06
这一题卡快排
by 君の名 @ 2021-07-15 15:51:57
STL大法。 nth_element(a,a+k,a+n);
by FunnyCreatress @ 2021-07-15 15:56:13
?上面说快排的是没看代码吗?快排只是lz表述不清
@Dgucheng
int f=a[(1+r)/2];
这个 1
是啥玩意
by Dgucheng @ 2021-07-15 22:12:12
@FunnyCreatress 啊!1和l搞混了,AC了!谢谢大佬!
by CHClFNO @ 2021-07-20 14:39:27
这题卡快排,建议吸氧+卡常