LIASPO @ 2022-03-23 11:03:10
#include<iostream>
#include<stdlib.h>
using namespace std;
int ans=0,k=1;
void qsort(int a[],int l,int r){
int i = l,j=r,flag=a[(l+r)/2],tmp;
if(r==l){
ans=a[l+1];
return;
}
do{
while(a[i]<flag)i++;
while(a[j]>flag)j--;
if(i<=j){
tmp=a[i];a[i]=a[j];a[j]=tmp;
i++;
j--;
}
}while(i<=j);
if(k<=j)qsort(a,l,j);
else if(i<=k)qsort(a,i,r);
else qsort(a,j+1,i-1);
}
int main(){
int a[250000]={0},l=1,r;
cin>>r>>k;
for(int i=1;i<=r;i++){
cin>>a[i];
}
qsort(a,l,r);
cout<<ans;
return 0;
}
by sw2022 @ 2022-03-23 11:10:02
@LIASPO n有
by Jerrlee✅ @ 2022-03-23 12:15:33
@LIASPO
数组要开大
cin 好像过不了,要用 scanf。
code:
#include<iostream>
#include<stdlib.h>
#include<cstdio>
using namespace std;
int ans=0,k=1;
void qsort(int a[],int l,int r){
int i=l,j=r,flag=a[(l+r)/2],tmp;
if(r==l){
ans=a[l+1];
return;
}
do{
while(a[i]<flag)i++;
while(a[j]>flag)j--;
if(i<=j){
tmp=a[i];a[i]=a[j];a[j]=tmp;
i++;
j--;
}
}while(i<=j);
if(k<=j)qsort(a,l,j);
else if(i<=k)qsort(a,i,r);
else qsort(a,j+1,i-1);
}
int main(){
int a[5000001],l=1,r;
scanf("%d%d",&r,&k);
for(int i=1;i<=r;i++) scanf("%d",&a[i]);
qsort(a,l,r);
printf("%d",ans);
}