SHANGSI @ 2021-07-24 10:40:58
#include<bits/stdc++.h>
using namespace std;
const int N=5000000;
int mun[N];
int fk(int k,int l,int r){
if(l==r)return mun[l];
int i=l,j=r,flag=mun[(l+r)/2];
while(i<=j){
while(mun[i]<flag)++i;
while(mun[j]>flag)--j;
if(i<=j)swap(mun[i++],mun[j--]);
}
if(k<=j)return fk(k,l,j);
else if(k>=i) return fk(k,i,r);
else return fk(k,j+1,i-1);
}
int main(){
int n,k;
cin>>n>>k;
for(int s=1;s<=n;++s){
scanf("%d",&mun[s]);
}
fk(k,1,n);
printf("%d",mun[k]);
return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int N=5000000;
int mun[N];
int fk(int k,int l,int r){
if(l==r)return mun[l];
int i=l,j=r,flag=mun[(l+r)/2];
while(i<=j){
while(mun[i]<flag)++i;
while(mun[j]>flag)--j;
if(i<=j)swap(mun[i++],mun[j--]);
}
if(k<=j)return fk(k,l,j);
else if(k>=i) return fk(k,i,r);
else return fk(k,j+1,i-1);
}
int main(){
int n,k;
cin>>n>>k;
for(int s=1;s<=n;++s){
scanf("%d",&mun[s]);
}
fk(k,1,n);
printf("%d",mun[k+1]);
return 0;
}
为什么加一就可以过,第k个元素不就是a[k]吗?
by _caiji_ @ 2021-07-24 10:45:21
读题