LLEeeeEe @ 2024-02-12 15:44:47
为什么我这个代码获得了3个WA 两个RE 求大佬们帮忙看看
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n,m,i,a[500000],b=0;
scanf("%d",&n);
cin>>m;
for(i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
for(i=0;i<n;i++)
{
if(a[i]!=a[i+1])
b++;
if(b==m)
cout<<a[i+1]<<endl;
break;
}
system("pause");
}
by cabbage_flower @ 2024-02-12 15:48:17
@LLEeeeEe 呃,数据范围是500万,你写的是50万
by shtian @ 2024-02-12 15:56:43
@LLEeeeEe 代码O(n * n),可以重写了
by shtian @ 2024-02-12 15:59:43
建议使用nth_element,O(n)
#include <bits/stdc++.h>
using namespace std;
int x[5000005], k;
int main() {
int n;
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++)
scanf("%d", &x[i]);
nth_element(x, x + k, x + n);
printf("%d", x[k]);
}