深海中的STL—nth_element
如果让你求区间第\(k\)大,你会怎么做呢?
主席树?确实是个不错的选择(不过像我这种垃圾还是乖乖打暴力吧)
在c++的stl库中,提供了nth_element这样一个函数
它的用法是nth_element(a+l,a+k,a+r)
这样它会使a这个数组中区间\([l,r)\)内的第\(k\)小的元素处在第\(k\)个位置上(相对位置)
但是它并不保证其他元素有序!
不过根据网友的实验,貌似在vs上是有序的,不过在dev中是无序的
时间复杂度:\(O(n)\)
#include<iostream>
#include<algorithm>
using namespace std;
int main() {
static int a[15] = {0, 1, 2, 5, 7, 3, 4, 1};
nth_element(a + 1, a + 4, a + 8);
for (int i = 1; i <= 8; i++) printf("%d ", a[i]); printf("\n");
return 0;
}
输出结果

深海中的STL—nth_element的更多相关文章
- 深海中的STL—mt19937
mt19937 当你第一眼看到这玩意儿的时候 肯定禁不住吐槽:纳尼?这是什么鬼? 确实,这个东西鲜为人知,但是它却有着卓越的性能 简介 mt19937是c++11中加入的新特性 它是一种随机数算法,用 ...
- hdu 6040 Hints of sd0061(stl: nth_element(arr,arr+k,arr+n))
Hints of sd0061 Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- c++ stl nth_element 原理解析
nth_element是stl中的一个库函数,它会使迭代器nth所指的元素与整个[first,last)完整排序后.同一个位置的元素同值.即找到完整排序时第n位的正确值. 但这个函数与完整排序的区别在 ...
- C++ STL nth_element
#include <iostream>#include <algorithm>#include <deque> using namespace std; int m ...
- USACO 刷题记录bzoj
bzoj 1606: [Usaco2008 Dec]Hay For Sale 购买干草——背包 #include<cstdio> #include<cstring> #incl ...
- 题解 UVA11300 【Spreading the Wealth】
环形均分纸牌问题应该不少人都很熟悉了,而且题解区写的也比较全了...... 我这篇题解主要是介绍一个新的STL--nth_element 以及解答几个其他题解里面有应用但是没有注释的问题.(比如说我第 ...
- 【穷竭】POJ3187-Backward Digit Sums
[思路] 利用杨辉三角形,每一个数字被加的次数等于它在杨辉三角形中对应的那个数字.注意这道题的意思是,最底层是N的全排序,而不是指1..10都可以.生成杨辉三角形的时候第一次我用了二重循环模拟生成,后 ...
- 18级北航软件学院算法复习--Samshui
A 比特手链 简单模拟 判断 贪心 叶姐要想哥赠送一串比特手链,这个手链由0和1组成.想哥买了手链B,无意间得知叶姐想要同样长度的手链A.想哥囊中羞涩,只能手工调整手链.他希望最少通过以下操作进行最少 ...
- STL 源码分析《2》----nth_element() 使用与源码分析
Select 问题: 在一个无序的数组中 找到第 n 大的元素. 思路 1: 排序,O(NlgN) 思路 2: 利用快排的 RandomizedPartition(), 平均复杂度是 O(N) 思路 ...
随机推荐
- webpack 解决 semantic ui 中 google fonts 引用的问题
semantic ui css 的第一行引用了 google web font api,由于不可告人而又众所周知的原因,这条链接在国内无法访问: @import url('https://fonts. ...
- 【安富莱二代示波器教程】第19章 附件E---参考资料
第19章 附件E---参考资料 DSP教程 http://forum.armfly.com/forum.php?mod=viewthread&tid=3886 . FreeRTOS教 ...
- SUSE12Sp3-Nginx安装
1.安装pcre(nginx 依赖) 把安装包pcre-8.12.tar.gz复制到服务器指定目录 tar -zxvf pcre-8.12.tar.gz # 解压 cd pcre-8.12 #进入目录 ...
- [Swift]LeetCode140. 单词拆分 II | Word Break II
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...
- [Swift]LeetCode470. 用 Rand7() 实现 Rand10() | Implement Rand10() Using Rand7()
Given a function rand7 which generates a uniform random integer in the range 1 to 7, write a functio ...
- [Swift]LeetCode1025. 除数博弈 | Divisor Game
Alice and Bob take turns playing a game, with Alice starting first. Initially, there is a number N o ...
- Python面试真题第二节
26.字符串a = "not 404 found 张三 99 深圳",每个词中间是空格,用正则过滤掉英文和数字,最终输出"张三 深圳" 27.filter方法求 ...
- postgresql 删除库的时候报错database "temp_test_yang" is being accessed by other users
删除库的时候报错 ERROR: database "temp_test_yang" is being accessed by other usersDETAIL: There ar ...
- Underscore.js 源码学习笔记(下)
上接 Underscore.js 源码学习笔记(上) === 756 行开始 函数部分. var executeBound = function(sourceFunc, boundFunc, cont ...
- Java9发布回顾Java 8的十大新特性
java9已经在北京时间9月22日正式发布,开发者可以在oracle jdk官网上下载到最新的jdk9. 今天,我们先来一起复习一下2014年发布的Java 8的十大新特性.先来喝杯java~~~ 按 ...