HDU-1387-Team Queue

HDU-1387-Team Queue

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

Team Queue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1259    Accepted Submission(s): 430




Problem Description
Queues and Priority Queues are data structures which are known to most computer scientists. The Team Queue, however, is not so well known, though it occurs often in everyday life. At lunch time the queue in front of the Mensa is a team queue, for example. 

In a team queue each element belongs to a team. If an element enters the queue, it first searches the queue from head to tail to check if some of its teammates (elements of the same team) are already in the queue. If yes, it enters the queue right behind them. If not, it enters the queue at the tail and becomes the new last element (bad luck). Dequeuing is done like in normal queues: elements are processed from head to tail in the order they appear in the team queue. 

Your task is to write a program that simulates such a team queue. 

 


Input
The input will contain one or more test cases. Each test case begins with the number of teams t (1<=t<=1000). Then t team descriptions follow, each one consisting of the number of elements belonging to the team and the elements themselves. Elements are integers in the range 0 – 999999. A team may consist of up to 1000 elements. 

Finally, a list of commands follows. There are three different kinds of commands: 

ENQUEUE x – enter element x into the team queue 

DEQUEUE – process the first element and remove it from the queue 

STOP – end of test case 

The input will be terminated by a value of 0 for t. 

 


Output
For each test case, first print a line saying “Scenario #k”, where k is the number of the test case. Then, for each DEQUEUE command, print the element which is dequeued on a single line. Print a blank line after each test case, even after the last one. 

 


Sample Input
   
   
2 3 101 102 103 3 201 202 203 ENQUEUE 101 ENQUEUE 201 ENQUEUE 102 ENQUEUE 202 ENQUEUE 103 ENQUEUE 203 DEQUEUE DEQUEUE DEQUEUE DEQUEUE DEQUEUE DEQUEUE STOP 2 5 259001 259002 259003 259004 259005 6 260001 260002 260003 260004 260005 260006 ENQUEUE 259001 ENQUEUE 260001 ENQUEUE 259002 ENQUEUE 259003 ENQUEUE 259004 ENQUEUE 259005 DEQUEUE DEQUEUE ENQUEUE 260002 ENQUEUE 260003 DEQUEUE DEQUEUE DEQUEUE DEQUEUE STOP 0

 


Sample Output
   
   
Scenario #1 101 102 103 201 202 203 Scenario #2 259001 259002 259003 259004 259005 260001

 


Source
 


这几天都在做栈和队列的题。刚刚起步。想多做点!

这个是一个模拟队列的题!

我之前用的链表写的。由于非常久没用链表了。可是这题用链表非常麻烦,确实。纠结我好久,可是还是写出来了,代码不是非常清晰,提交一遍然后TLE了,唉:-(


然后网上看了看别人的代码,发现自己还有非常多不足之处!

!所以说新手还是得多看看别人的代码啊!那个代码思路还是非常清晰的,先贴在这里,以后再来回顾回顾!



我的TLE代码(链表:没用c++里的queue写,一是想练习一下链表。二是想模拟那个过程):


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
using namespace std;

typedef struct que
{
	int da;
	struct que* next;
}*queu, node;

struct fun
{
	int da;
	int te;
}te_me[1000000];

int me_num;

int Len(queu q)
{
	int len=0;
	while(q->next)
	{
		q = q->next;
		len++;
	}
	return len;
}

int query(int a)
{
	for(int i=0; i<me_num; i++)
		if(a == te_me[i].da)return te_me[i].te; 
}

int Entry_q(int a, queu q, int len)
{
	int team = query(a);
	for(int i=0; i < len; i++)
	{
		if(team == query(q->da)  && team != query(q->next->da))
		{
			queu p = (node*)malloc(sizeof(node)); p->next=NULL;
			p->da = a;
			p->next = q->next;
			q->next = p;
			return 1;
		}
		else q = q->next;
	}
	return 0;
}

int main()
{
	int T, count=0;
	while(scanf("%d", &T), T)
	{
		count++;
		int n; me_num=0;
		for(int k = 1; k <= T; k++)
		{
			scanf("%d", &n); int m;
			while(n--)
			{
				scanf("%d", &m);
				te_me[me_num].da = m;
				te_me[me_num].te = k;
				me_num++;
			}
		}
		printf("Scenario #%d\n", count);
		queu q = (node*)malloc(sizeof(node)); q->next=NULL;
		queu front=q, rear=q, p;
		char fun[10]; int elem;
		while(scanf("%s", fun)!=EOF)
		{
			if(!strcmp(fun, "ENQUEUE")){
				scanf("%d", &elem);
				if(front == rear){
					rear->da = elem;
					p = (node*)malloc(sizeof(node)); p->next=NULL;
					rear->next = p; rear = p;
				}
				else if(!Entry_q(elem, front, Len(front))) {
					rear->da = elem;
					p = (node*)malloc(sizeof(node)); p->next=NULL;
					rear->next = p; rear = p; 
				}
			}
			else if(!strcmp(fun, "DEQUEUE")){
				if(front->da)printf("%d\n", front->da);
				queu fe=front;
				front = fe->next;
				free(fe);
			}
			else if(!strcmp(fun, "STOP")){
				break;
			}
		}
		printf("\n");
	}
	return 0;
} 


AC代码(93ms):


#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

#define	MAX_RANK 1000000
#define MAX_QUE  1000
#define MAX_N    1000
#define CMD_CHAR 30

int team[MAX_RANK];
queue<int> que[MAX_QUE];
queue<int> bigQue;
void init();
int main()
{
	int cases = 1;

	int teamM;
	while (scanf("%d", &teamM) == 1 && teamM) {
		// init
		init();

		// enter team
		int n;
		memset(team, 0, sizeof(team));
		for (int team_NO = 0; scanf("%d", &n) == 1; team_NO++) {
			for (int i = 0; i < n; i++) {
				int num;
				scanf("%d%*c", &num);
				team[num] = team_NO;
			}
		}

		// read commands
		printf("Scenario #%d\n", cases++);
		while (true) {
			char cmd[CMD_CHAR];
			scanf("%s", cmd);

			if (strcmp(cmd, "ENQUEUE") == 0) {
				int num;	
				scanf("%d%*c", &num);

				if (que[team[num]].empty()) {
					bigQue.push(team[num]);
				}
				que[team[num]].push(num);
			} else if (strcmp(cmd, "DEQUEUE") == 0) {
				int whitch_team = bigQue.front();
				printf("%d\n", que[whitch_team].front());
				que[whitch_team].pop();
				if (que[whitch_team].empty()) {
					bigQue.pop();
				}
			} else {
				printf("\n");
				break;
			}
		}
		
	}

	return 0;
}
void init()
{
	while (!bigQue.empty()) {
		bigQue.pop();
	}

	for (int i = 0; i < MAX_QUE; i++) {
		while (!que[i].empty()) {
			que[i].pop();
		}
	}
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117438.html原文链接:https://javaforall.net

(0)
上一篇 2022年1月5日 下午7:00
下一篇 2022年1月5日 下午8:00


相关推荐

  • linux卸载PyCharm

    linux卸载PyCharmDeletetheins Removethefol Syntax config JetBrains product version cache JetBrains product version local share JetBrains product version Example version product version product version product

    2026年3月26日
    3
  • BMP文件格式详解

    BMP文件格式详解BMP 文件格式详解 BMPfileforma BMP 文件格式 又称为 Bitmap 位图 或是 DIB Device IndependentD 设备无关位图 是 Windows 系统中广泛使用的图像文件格式 由于它可以不作任何变换地保存图像像素域的数据 因此成为我们取得 RAW 数据的重要来源 Windows 的图形用户界面 graphicaluse

    2026年3月18日
    2
  • HTTP状态500-内部服务器错误[通俗易懂]

    HTTP状态500-内部服务器错误[通俗易懂]SSM整合时出现的问题起初关注点一直在Mapper.xml上,CSDN查询解决方法大致在这样几种。1.SQL语句问题2.resultType返回类型问题(List集合中的参数类型)3.database.properties数据库配置文件问题(Mysql8以上加时区,文件多写了空格,少写了jdbc.)在看到Mysql8加时区这个解决方法时仔细观察了一下报错信息java.lang.ClassNotFoundException:com.mysql.cj.jdbc.Driver会不会是jar包问.

    2022年8月11日
    14
  • Jupyter notebook 报错 500 : Internal Server Error的解决方法「建议收藏」

    Jupyter notebook 报错 500 : Internal Server Error的解决方法「建议收藏」问题:输入jupyternotebook后再浏览器点击.ipynb文件报错500InternalServerError,异常如下图所示解决方法:1).先卸载jupyter并删除安装目录下的以jupyter开头的文件,再重新pipinstalljupyter安装jupyter,试验后再打开jupyternotebook,仍无法正常打开.i…

    2022年7月12日
    21
  • 验证服务器端口telnet,telnet命令测试服务器端口是否通的办法

    验证服务器端口telnet,telnet命令测试服务器端口是否通的办法telnet 命令测试服务器端口是否通的办法很多站长服务器端口并不清楚怎么测试是否通 下面就写一篇测试服务器端口是否正常通讯的教程 本教程以 Windows 本机为例 可测试各类操作系统的服务器 首先我们电脑上需要安装一个 telnet 服务 安装方法如下 打开控制面板控制面板然后点击 程序和功能 点击启用或关闭 Windows 功能控制面板 程序和功能在弹出的程序和功能列表里勾选上 telnet 客户端这一项

    2026年3月17日
    3
  • js的数据类型有哪些?[通俗易懂]

    js的数据类型有哪些?[通俗易懂]数据类型一、数据类型:基本数据类型(值类型):字符串(String)、数字(Number)、布尔(Boolean)、对空(Null)、未定义(Undefined)。引用数据类型(对象类型):对象(Object)、数组(Array)、函数(Function)。特殊的对象:正则(RegExp)和日期(Date)。特殊类型:underfined未定义、Null空对象、Infinate无穷、NAN非数字基本数据类型的值直接在栈内存中存储,值与值之间独立存在,修改一个变量不会影响.

    2025年9月19日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号