multiset应用

网友投稿 657 2022-10-22

multiset应用

multiset应用

multiset和set是集合类,set不能有重复元素,multiset可以有重复元素,均是由平衡二叉树支持的。以下来自于百度百科:平衡二叉树(Balanced Binary Tree)又被称为AVL树(有别于AVL算法),且具有以下性质:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。构造与调整方法 平衡二叉树的常用算法有红黑树、AVL、Treap等。 最小二叉平衡树的节点的公式如下 F(n)=F(n-1)+F(n-2)+1 这个类似于一个递归的数列,可以参考Fibonacci数列,1是根节点,F(n-1)是左子树的节点数量,F(n-2)是右子树的节点数量。 C++ Multisets 常用函数:begin(),clera(),empty(),end(),erase(),insert(),size(),swap()等。 multiset和set一样,能同时访问头和尾(不像队列啊~),更重要的是两者可以对元素排好序,这样就能方便的对极值操作。 优先队列是依据堆(二叉树)实现的,效率是O(lgn),multiset是由平衡二叉树支持的。二叉查找树有一种极端情况:当所有节点位于一条链上时,二叉查找树的操作时间为O(N),而平衡二叉树保证了在最坏情况下集合操作的时间复杂度是O(lgn). 一个经典的例子:

Problem Description

A simple problemProblem DescriptionYou have a multiple set,and now there are three kinds of operations:1 x : add number x to set2 : delete the minimum number (if the set is empty now,then ignore it)3 : query the maximum number (if the set is empty now,the answer is 0)

Input

N ( N≤106),representing the number of operations.Next N line ,each line contains one or two numbers,describe one operation.The number in this set is not greater than 109.

Output

For each operation 3,output a line representing the answer.

Sample Input

61 21 331 31 43

Sample Output

34

代码

#include #include#includeusing namespace std;multiset mul;multiset::iterator ix;int main(){ //freopen("cin.txt","r",stdin); int n,number; while(cin>>n){ mul.clear(); for(int i=0;i

用优先队列尝试了下,错了,why~~

#include #include#include#includeusing namespace std;const int maxn=1e6+5,INF=0x3f3f3f3f;int main(){ //freopen("cin.txt","r",stdin); int n,number; while(cin>>n){ priority_queue,greater > que; int maxm=-INF; for(int i=0;i

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:解决Spring Security的权限配置不生效问题
下一篇:微软Windows MFC42,MFC71重要API发现0day漏洞
相关文章

 发表评论

暂时没有评论,来抢沙发吧~