nth_element()
来源:互联网 发布:驾驶员网络培训 编辑:程序博客网 时间:2024/06/03 12:40
原题:
输入
多组输入数据
第一行为正整数n(1 <= n <= 100000)
接下来n个int范围内的正整数
输出
对于每组数据,输出一行,为这n个数中的的中位数
解析:
本题旨在快速求解中位数,考虑使用STL中位于<algorithm>库下的element(start,start+n,end)函数,其作用是使第n大元素处于第n位置,并且比这个元素小的排在这个元素前面,比这个元素大的排在后面。对于本题,令n为中位数就能快速高效通过。
代码:
#include<cstdio>#include<algorithm>using namespace std;int a[100007];int main(){ int n; while(~scanf("%d",&n)) { int mid = n%2 == 1 ? n/2 : n/2-1; for(int i = 0;i < n;i++) { scanf("%d",&a[i]); } nth_element(a,a+mid,a+n); printf("%d\n",a[mid]); }}
阅读全文
0 0
- nth_element()
- nth_element()方法总结
- nth_element
- nth_element
- 2825赛车比赛(nth_element)
- HDU 6040 Hints of sd0061(nth_element)
- top K算法总结(附带STL nth_element的分析)
- HDU 6040 Hints of sd0061(STL nth_element函数)
- std::nth_element
- nth_element使用
- #1133 : 二分·二分查找之k小数 ( 快速排序, 分治 OR nth_element() 函数)
- STL之nth_element()(取容器中的第n大值)
- HDU 6040 Hints of sd0061 找第K大数 (nth_element函数)
- stl nth_element使用...
- STL源码解析 - nth_element
- nth_element的用法
- partial_sort,partial_sort_copy,nth_element
- std::nth_element 排序
- openssl 自建ca,颁发客户端证书
- POJ 3177
- 题目353:3D dungeon
- hdu5534 Partial Tree (巧妙转为完全背包)
- Java学习之基础训练二-----数组定义、赋值、求最值、排序、复制、扩容
- nth_element()
- console.log 打印object
- HDU 5521 Meeting(最短路)
- gcc的详细用法
- oracle锁与死锁概念,阻塞产生的原因以及解决方案
- water 灌水问题(最小生成树 搜索)
- 页面不缓存设置
- jenkins --部署项目到服务器需要的脚本
- MATLAB图像处理基础知识3 替换矩阵的数值A(A<0) = 1