One_last_Spider_man @ 2023-08-28 21:02:29
原代码(#5 1130ms):
#include <bits/stdc++.h>
using namespace std;
int k;
int a[5000005];
void sort(int l,int r) {
int i = l,j = r,flag =a[(l+r)/2];
do {
while(a[i] < flag) i++;
while(a[j] > flag) j--;
if(i <= j)
{
swap(a[i],a[j]);
i++;j--;
}
}while(i<=j);
if(k+1 <= j) sort(l,j);
else if(k+1 >= i) sort(i,r);
else{
printf("%d" , a[k+1]);
exit(0);
}
}
int main(){
int n;
scanf("%d%d" , &n , &k);
for(int i=1;i<=n;++i) scanf("%d" , &a[i]);
sort(1,n);
return 0;
}
该了个数(#5 343ms):
#include <bits/stdc++.h>
using namespace std;
int a[5000005],k;
void sort(int l,int r) {
int i = l,j = r,flag =a[(l+r)/2];
do {
while(a[i] < flag) i++;
while(a[j] > flag) j--;
if(i <= j)
{
swap(a[i],a[j]);
i++;j--;
}
}while(i<=j);
if(k <= j) sort(l,j);
else if(k >= i) sort(i,r);
else{
printf("%d" , a[j+1]);
exit(0);
}
}
int main(){
int n;
scanf("%d%d" , &n , &k);
for(int i=0;i<n;++i) scanf("%d" , &a[i]);
sort(0,n-1);
return 0;
}
by One_last_Spider_man @ 2023-08-28 21:06:32
忘说了,解疑者赏关注QwQ
by Rain_G @ 2023-08-28 21:21:25
啊?
by Rain_G @ 2023-08-28 21:22:30
玄学问题,我看了好久,同样的码,我这边就能过
by glx123 @ 2023-08-28 21:24:32
对啊,不是本来就能过吗
by Rain_G @ 2023-08-28 21:24:37
但时间比你快了很多
by One_last_Spider_man @ 2023-08-28 21:40:16
啊?
by One_last_Spider_man @ 2023-08-28 21:56:47
洛谷滴问题! @ Rain_G 又试了下就过了,你发现并提出了问题,所以发现它是对滴,你也就算解决了问题, 所以赏@ Rain_G 一个大关注 (滑稽)