hdu 1873(看病要排队)

149 篇文章 0 订阅
15 篇文章 0 订阅

题目大意:这道题是中文题,读者可直接去OJ上看题目

解题思路:题意并不难理解。在我们的现实生活中,假如我们要找某一个医生看病,是不是就要到他的那一条队列上去排队???

而这个队列又能根据多种情况来排序,这时候,我们可以考虑用以下优先队列。。。。


代码如下:

版本一(400MS左右):

/*
 * 1873_2.cpp
 *
 *  Created on: 2013年8月8日
 *      Author: Administrator
 */

#include <iostream>
#include <queue>

using namespace std;

struct Node {
	int pri;
	int num;
public:
	friend bool operator<(const Node& a, const Node& b) {
		if (a.pri != b.pri) {
			return a.pri < b.pri;
		}

		return a.num > b.num;
	}
};

int main() {

	int n;

	while (cin >> n) {
		priority_queue<Node> q[4];
        int b,count = 1;
        string str;
        Node a;
		for (int i = 0; i < n; ++i) {


			cin >> str;



			if (str == "IN") {
				cin >> b >> a.pri;
				a.num = count++;

				q[b].push(a);

			} else {
				cin >> b;
				if (!q[b].empty()) {
					a = q[b].top();
					q[b].pop();
					cout<<a.num<<endl;
				}else{
					cout<<"EMPTY"<<endl;
				}
			}
		}
	}
}




版本二:

/*
 * 1873_1.cpp
 *
 *  Created on: 2013年8月8日
 *      Author: Administrator
 */

#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"queue"
using namespace std;
int tot;
struct node {
	int pri;
	int num;
	friend bool operator<(node n1, node n2) {
		if (n1.pri == n2.pri)
			return n2.num < n1.num;
		else
			return n1.pri < n2.pri;
	}
};
int main() {
	int n;
	node now;
	int a, b;
	char str[20];
	while (scanf("%d", &n) != -1) {
		priority_queue<node> q[4];
		tot = 0;
		while (n--) {
			scanf("%s", str);
			if (strcmp(str, "OUT") == 0) {
				scanf("%d", &a);
				if (q[a].empty())
					printf("EMPTY\n");
				else {
					now = q[a].top();
					q[a].pop();
					printf("%d\n", now.num);
				}
			} else if (strcmp(str, "IN") == 0) {
				scanf("%d%d", &a, &b);
				now.num = ++tot;
				now.pri = b;
				q[a].push(now);
			}
		}
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气的东哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值