(优先队列)HDU1873 看病要排队

题目链接:(优先队列)HDU1873 看病要排队

题解:数值不大,直接模拟实现的。当然也可以用优先队列,见第二个代码。

代码1:直接模拟。

//直接模拟 
#include<iostream>
#include<queue>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
const int maxn = 2010;
const int INF=0x3f3f3f3f;

typedef struct Node{
    int x,y;
}Node;
Node mes[maxn];
int vis[maxn];

int main()
{
    char s[5];
    int n,m;
    while(~scanf("%d",&n)){
        memset(vis,0,sizeof(vis));
        int r=1;
        while(n--){
            scanf("%s",s);
            if(strcmp(s,"OUT")==0){
                scanf("%d",&m);
                int mx=0,ans;
                for(int i=1;i<r;i++){
                    if(!vis[i]&&mes[i].x==m&&mx<mes[i].y){
                        mx=mes[i].y;
                        ans=i;
                    }
                }
                if(mx==0){
                    printf("EMPTY\n");
                }else{
                    vis[ans]=1;
                    printf("%d\n",ans);
                }
            }else{
                scanf("%d%d",&mes[r].x,&mes[r].y);
                r++;
            }
        }
    }
    return 0;
}

代码 2:优先队列实现。

注意优先队列比较函数的写法,正好跟一般结构体的相反。

#include<iostream>
#include<queue>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
const int maxn = 2010;
const int INF=0x3f3f3f3f;

typedef struct Node{
	int y,id;
	bool operator < (const Node &A)const{
		if(y!=A.y) return y<A.y;
		return id>A.id;
	}
}Node;
Node tmp;

int main()
{
    char s[5];
    int n,m,x;
    while(~scanf("%d",&n)){
    	priority_queue<Node> pq[4];
    	int r=1;
    	while(n--){
	    	scanf("%s",s);
	    	if(strcmp(s,"OUT")==0){
	    		scanf("%d",&m);
				if(pq[m].empty()){
					printf("EMPTY\n");
				}else{
					printf("%d\n",pq[m].top().id);
					pq[m].pop();
				}
			}else{
				scanf("%d%d",&x,&tmp.y);
				tmp.id=r;
				pq[x].push(tmp);
				r++;
			}
		}
	}
    return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值