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();
		}
	}
}

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

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • dota5显示正在连接协调服务器,win10系统打开dota2提示已连接至DOTA2游戏协调服务器正在登陆中如何解决…

    dota5显示正在连接协调服务器,win10系统打开dota2提示已连接至DOTA2游戏协调服务器正在登陆中如何解决…近日有win10系统用户要在电脑中玩dota2游戏的时候,发现一打开dota2提示已连接至DOTA2游戏协调服务器正在登陆中,该怎么办呢,本文就给大家讲解一下win10系统打开dota2提示已连接至DOTA2游戏协调服务器正在登陆中的详细解决步骤。解决方法一、1、开始按钮在搜索中输入CMD,打开第一个在弹出框输入:netshwinsockresetcatalog;2、直接复制然后再弹出框中右…

  • mysql 字符串索引 起始_mysql截取字符串「建议收藏」

    mysql 字符串索引 起始_mysql截取字符串「建议收藏」mysql截取字符串mysql索引从1开始一、mysql截取字符串函数1、left(str,index)从左边第index开始截取2、right(str,index)从右边第index开始截取3、substring(str,index)当index>0从左边开始截取直到结束当index<0从右边开始截取直到结束当index=0返回空4、substring(str,index,…

  • pycharm2021专业版永久激活码【2021最新】

    (pycharm2021专业版永久激活码)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.cn/100143.html…

  • 因果图分析法[通俗易懂]

    因果图分析法[通俗易懂]目录一、因果图法1.理解二、因果图需要掌握的基本知识1.关系2.约束3.输出条件的约束4.输出条件的约束5.原因和结果表示6.中间节点三、因果图设计测试用例的步骤四、优缺点1.优点2.缺点五、实例1.案例2.分析案例六、为什么要有中间节点1.无中间节点因果图2.有中间节点因果图一、因果图法1.理解因果图是一种简化了的逻辑图,能直观的表明程序输入条件(原因)和输出动作(结果)之间的相互关系; 因果图法是借助图形来设计测试

  • 前端vue面试题2021_vue框架面试题

    前端vue面试题2021_vue框架面试题一.自我介绍(我是谁来自哪里,今天来的目的,面试的岗位是什么,几年的工作经验,掌握的技术栈有哪些,开发过什么项目,项目中负责的板块是什么)面试官您好!我叫XXX,来自XXX,很荣幸能来我们公司面试,我从事前端开发有3年了,目前掌握的技术有html,css,js,ajax,vue,小程序,参与过各种类型的项目。我做过的项目有A,B,C,D,E那么最近做的一个项目是XXX在这个项目中我主要负责的板块是XXX面试官您这边还有什么想要了解的么。二.项目功能提问vue后台项目(这几个功能点要求

  • jsoup 1.5.2 发布,超棒的 HTML 解析器

    jsoup 1.5.2 发布,超棒的 HTML 解析器

发表回复

您的电子邮箱地址不会被公开。

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