HDU 1434(幸福列车)

每辆列车对应一个优先队列,按要求入队列、出队列、合并队列即可,详细见注释。

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int MAXN = 10005;

struct passenger //乘客
{
	char name[25]; //姓名
	int RP; //人品值
	bool operator<(const passenger& p) const //优先队列排序标准
	{
		if (RP != p.RP) //人品值不相等时,人品值高的排在后面
			return RP > p.RP;
		else //人品值相等时,姓名字典序在前的排在后面
			return strcmp(name, p.name) < 0;
	}
};

int main()
{
	int N, M;
	while (scanf("%d%d", &N, &M) != EOF)
	{
		priority_queue<passenger> q[MAXN]; //优先队列数组

		int num; //每辆列车的乘客数量
		for (int i = 1; i <= N; i++)
		{
			scanf("%d", &num);
			while (num--) //向列车i中填入乘客
			{
				passenger p;
				scanf("%s%d", p.name, &p.RP);
				q[i].push(p);
			}
		}

		char command[10]; //命令
		int Xi, Xj;
		for (int i = 0; i < M; i++)
		{
			scanf("%s", command);
			if (!strcmp(command, "GETON"))
			{
				scanf("%d", &Xi);
				passenger p;
				scanf("%s%d", p.name, &p.RP);
				q[Xi].push(p);
			}
			else if (!strcmp(command, "JOIN"))
			{
				scanf("%d%d", &Xi, &Xj);
				passenger p;
				while (!q[Xj].empty())
				{
					p = q[Xj].top();
					q[Xj].pop();
					q[Xi].push(p);
				}
			}
			else if (!strcmp(command, "GETOUT"))
			{
				scanf("%d", &Xi);
				printf("%s\n", q[Xi].top().name);
				q[Xi].pop();
			}
		}
	}
	return 0;
}

继续加油。

发布了326 篇原创文章 · 获赞 1 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Intelligence1028/article/details/105313130
hdu
今日推荐