multiset在排队问题中的应用

本文展示了一个使用C++进行编程竞赛的例子,通过一个具体的题目,介绍了如何利用multiset及自定义比较器来实现病人的排队和就诊逻辑。具体包括病人信息的结构体定义、基于优先级的排序规则定制、以及对不同医生队列的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目: https://vjudge.net/contest/220145#problem/C

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
using namespace std;
struct br{
 int y;
 int id;
};
struct cmp//降序排序
{
 bool operator ()(br a,br b)
 {
  return a.y>b.y;
 }
 
};
int main()
{
 int n;
 while(scanf("%d",&n)==1)
 {
  multiset<br,cmp> a[4];//代表3个医生的队伍
  multiset<br,cmp>::iterator it;
  string c;
  int q,w;
  br x;
  int j=0;
  while(n--)
  {
   cin >> c;
   if(c=="IN")
   {
    j++;
    cin >> w >> q;
    x.y=q;x.id=j;
    a[w].insert(x);//相等时,后来的排在后面(方便)
   }
   else
   {
    cin >> w;
    if(a[w].size()==0){
     cout << "EMPTY" << endl;
    }
    else
    {
     it=a[w].begin();//begin就是开头
     cout << it->id << endl;
     a[w].erase(it);//只会删掉这个元素,和它相等的不会删
    }
   }
  }
 }
 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值