用处:
用来找第k小的数。
举例:
找一个序列中第k小的数.
代码:
#include<bits/stdc++.h>
#include<map>
#define endl '\n'
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 5e6 + 10;
int a[N];
int main()
{
int n,k;
scanf("%d %d",&n,&k);
for(int i = 0 ; i < n ; i ++) scanf("%d",&a[i]);
nth_element(a, a + k, a + n);
printf("%d\n",a[k - 1]);
return 0;
}
本文介绍了一种使用C++实现的高效算法,用于在未排序序列中查找第K小的元素。通过使用标准模板库(STL)中的nth_element函数,可以快速定位目标元素而无需完全排序整个序列。
273

被折叠的 条评论
为什么被折叠?



