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;
}



### C++ `multiset` 容器的特性 `multiset` 是 C++ 标准模板库(STL)中的关联容器之一,允许存储多个具有相同键值的元素。与 `set` 不同的是,`set` 中不允许存在重复元素,而 `multiset` 可以容纳多个相同的元素[^1]。 #### 创建初始化 可以通过多种方式来创建并初始化 `multiset`: ```cpp #include <iostream> #include <set> using namespace std; int main() { // 初始化 multiset 并插入一些初始元素 multiset<int> ms = {1, 2, 3, 3}; // 插入更多元素 ms.insert(4); ms.insert(3); // 打印 multiset 的所有元素 cout << "multiset 容器的元素为:" << endl; for (auto value : ms){ cout << value << "\t"; } } ``` 这段代码展示了如何定义一个整数类型的 `multiset`,并通过列表初始化它,之后还可以继续向其中添加新元素[^3]。 #### 访问元素 由于 `multiset` 自动保持内部元素按升序排列,所以无法通过索引来访问特定位置上的元素。但是,可以遍历整个集合或利用迭代器定位某个范围内的元素。 #### 删除元素 要移除指定值的所有实例,可调用 erase 函数: ```cpp ms.erase(value); // 移除所有的 'value' // 或者只删除一次出现 auto pos = ms.find(value); if(pos != ms.end()){ ms.erase(pos); // 移除找到的第一个匹配项 } ``` 需要注意的是,在执行上述操作时,如果目标值不存在于 `multiset` 中,则不会有任何效果;对于单个元素的删除,建议先查找再删除以确保效率。 #### 查找元素 为了高效地检索某值是否存在以及其数量,提供了 count 方法: ```cpp size_t nOccurrences = ms.count(keyValue); // 返回 key 值的数量 bool exists = (nOccurrences > 0); ``` 此功能特别适用于统计分析等应用场景中,因为可以直接获取某一特定值在整个序列里出现了多少次。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值