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 problem Problem Description You have a multiple set,and now there are three kinds of operations: 1 x : add number x to set 2 : 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
  
  
The first line contains a number N ( N106 ),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
  
  
6 1 2 1 3 3 1 3 1 4 3
 

Sample Output
  
  
3 4
代码:
#include <iostream>
#include<cstdio>
#include<set>
using namespace std;
multiset<int> mul;
multiset<int>::iterator ix;
int main()
{
    //freopen("cin.txt","r",stdin);
    int n,number;
    while(cin>>n){
         mul.clear();         
         for(int i=0;i<n;i++){
             int id;
             scanf("%d",&id);
             if(id==1){
                 scanf("%d",&number);
                 mul.insert(number);
             }
             else if(id==2){
                 if(!mul.empty()){
                     mul.erase(mul.begin());
                 }
             }
             else {
                  if(mul.empty())printf("0\n");
                  else {
                      ix=mul.end();
                      printf("%d\n",*(--ix));
                  }
             }
         }
    }
    return 0;
}
用优先队列尝试了下,错了,why~~
#include <iostream>
#include<cstdio>
#include<queue>
#include<vector>
using namespace std;
const int maxn=1e6+5,INF=0x3f3f3f3f;
int main()
{
    //freopen("cin.txt","r",stdin);
    int n,number;
    while(cin>>n){
         priority_queue<int,vector<int>,greater<int> > que;
         int maxm=-INF;
         for(int i=0;i<n;i++){
             int id;
             scanf("%d",&id);
             if(id==1){
                 scanf("%d",&number);
                 que.push(number);
                 if(maxm<number)maxm=number;
             }
             else if(id==2){
                 if(!que.empty()){
                     que.pop();
                 }
             }
             else {
                  if(que.empty())printf("0\n");
                  else printf("%d\n",maxm);
             }
         }
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值