#include <algorithm>
// 从0开始,查找第三小的元素
nth_element(v.begin(), v.begin() + 2, v.end());
cout << "第3小的元素是: " << v[2] << endl;

如果遇到大量的数据,直接使用可能会导致超时,需添加

cin.tie(nullptr); // 禁用cout缓冲区刷新, 输出需带上cout << "" << flush;
ios::sync_with_stdio(false); // 禁用 C++ 标准流与 C 标准流的同步,无法使用scanf等

这将显著提升性能

haihaihai

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注