#include <algorithm>

SGI STL,DEV C++4.9.9.2

nth_element作用为求第n小的元素,并把它放在第n位置上,下标是从0开始计数的,也就是说求第0小的元素就是最小的数。

例子:

int main()
{
    int a[]={3,2,1};
    nth_element(a,a+0,a+9);
    cout<<a[0]<<endl;
    system("pause");
    return 0;
}
输出为1。

整体上说,如果只是求第n大或者第n小的元素,可以采用这个算法,要优化很多。nth_element不会对整体进行排序。

http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1097

直接就可以过!

相关文章:

  • 2022-12-23
  • 2022-02-27
  • 2022-12-23
  • 2021-12-23
  • 2021-08-14
  • 2022-12-23
  • 2021-09-26
猜你喜欢
  • 2022-12-23
  • 2021-06-13
  • 2021-10-10
  • 2022-12-23
  • 2021-07-16
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案