UVA_540: Team Queue

本文介绍了一种特殊的数据结构——团队队列,并通过一个具体的编程实现案例来解释如何模拟这种队列的行为。团队队列中,元素按团队进行组织,同一团队的元素在加入或离开队列时会保持相对顺序。

Description

Download as PDF

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 file will contain one or more test cases. Each test case begins with the number of teams t ( $1 \le t \le 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.


Warning: A test case may contain up to 200000 (two hundred thousand) commands, so the implementation of the team queue should be efficient: both enqueing and dequeuing of an element should only take constant time.

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


分析:本题有两个队列,每个团队有一个队列,团队整体又是一个队列。把题中的队列按团队分开,其实团队队列就是一个索引,每次插入删除都要更新团队队列


#include <cstdio>
#include <map>
#include <queue>

using namespace std;

const int maxn = 1000 + 10;

int main()
{
	int t,kase = 0;
	while(~scanf("%d",&t)&&t)
	{
		printf("Scenario #%d\n",++kase);
		map<int,int> team;
		for(int i=0; i<t; i++)
		{
			int n,x;
			scanf("%d",&n);
			while(n--) {scanf("%d",&x); team[x]=i;}
		}

		char op[10];
		queue<int> q,q2[maxn];
		while(~scanf("%s",op))
		{
			if(op[0]=='S')break;
			if(op[0]=='D'){
				int top = q.front();
				printf("%d\n",q2[top].front());
				q2[top].pop();
				if(q2[top].empty())q.pop();
			}else if(op[0]=='E'){
				int x;
				scanf("%d",&x);
				int t = team[x];
				if(q2[t].empty()){q.push(t);}
				q2[t].push(x);
			}
		}
		putchar('\n');
	}
    return 0;
}


#include "ESP32SPISlave.h" static spi_slave_transaction_t t; void ESP32SPISlave::begin(int8_t sck, int8_t miso, int8_t mosi, int8_t cs, int8_t channel) { spi_bus_config_t buscfg = { .mosi_io_num = mosi, .miso_io_num = miso, .sclk_io_num = sck, .quadwp_io_num = -1, .quadhd_io_num = -1 }; spi_slave_interface_config_t slvcfg = { data_mode, // mode cs, // spics_io_num 0, // flags queue_size // queue_size }; // 使用显式的 spi_host_device_t 类型转换 esp_err_t ret = spi_slave_initialize((spi_host_device_t)channel, &buscfg, &slvcfg, SPI_DMA_CH_AUTO); if (ret != ESP_OK) { log_e("spi_slave_initialize failed: %d", ret); } } void ESP32SPISlave::setDataMode(uint8_t mode) { data_mode = mode; } void ESP32SPISlave::setQueueSize(uint32_t size) { queue_size = size; } size_t ESP32SPISlave::transfer(uint8_t* tx_buf, uint8_t* rx_buf, size_t len, TickType_t timeout) { memset(&t, 0, sizeof(t)); t.length = len * 8; t.tx_buffer = tx_buf; t.rx_buffer = rx_buf; esp_err_t ret = spi_slave_transmit(SPI2_HOST, &t, timeout); return (ret == ESP_OK) ? len : 0; } ESP-ROM:esp32s3-20210327 Build:Mar 27 2021 rst:0x1 (POWERON),boot:0x28 (SPI_FAST_FLASH_BOOT) SPIWP:0xee mode:DIO, clock div:1 load:0x3fce3808,len:0x4bc load:0x403c9700,len:0xbd8 load:0x403cc700,len:0x2a0c entry 0x403c98d0 E (6) spi_slave: spi_slave_initialize(116): invalid host [ 104][E][ESP32SPISlave.cpp:25] begin(): spi_slave_initialize failed: 258 SPI Slave initialized. Awaiting data...SPI Slave Task Running. Will respond with enhanced packet after receiving 256 bytes. E (174) spi_slave: spi_slave_queue_trans(286): host not slave E (175) spi_slave: spi_slave_queue_trans(286): host not slave E (176) spi_slave: spi_slave_queue_trans(286): host not slave E (213) spi_slave: spi_slave_queue_trans(286): host not slave E (214) spi_slave: spi_slave_queue_trans(286): host not slave E (215) spi_slave: spi_slave_queue_trans(286): host not slave E (253) spi_slave: spi_slave_queue_trans(286): host not slave E (254) spi_slave: spi_slave_queue_trans(286): host not slave E (255) spi_slave: spi_slave_queue_trans(286): host not slave E (293) spi_slave: spi_slave_queue_trans(286): host not slave E (294) spi_slave: spi_slave_queue_trans(286): host not slave E (295) spi_slave: spi_slave_queue_trans(286): host not slave E (333) spi_slave: spi_slave_queue_trans(286): host not slave E (334) spi_slave: spi_slave_queue_trans(286): host not slave E (335) spi_slave: spi_slave_queue_trans(286): host not slave E (374) spi_slave: spi_slave_queue_trans(286): host not slave E (375) spi_slave: spi_slave_queue_trans(286): host not slave E (376) spi_slave: spi_slave_queue_trans(286): host not slave E (413) spi_slave: spi_slave_queue_trans(286): host not slave E (414) spi_slave: spi_slave_queue_trans(286): host not slave E (415) spi_slave: spi_slave_queue_trans(286): host not slave E (453) spi_slave: spi_slave_queue_trans(286): host not slave E (454) spi_slave: spi_slave_queue_trans(286): host not slave E (455) spi_slave: spi_slave_queue_trans(286): host not slave E (493) spi_slave: spi_slave_queue_trans(286): host not slave E (494) spi_slave: spi_slave_queue_trans(286): host not slave E (495) spi_slave: spi_slave_queue_trans(286): host not slave E (533) spi_slave: spi_slave_queue_trans(286): host not slave E (534) spi_slave: spi_slave_queue_trans(286): host not slave E (535) spi_slave: spi_slave_queue_trans(286): host not slave E (574) spi_slave: spi_slave_queue_trans(286): host not slave E (575) spi_slave: spi_slave_queue_trans(286): host not slave E (576) spi_slave: spi_slave_queue_trans(286): host not slave E (614) spi_slave: spi_slave_queue_trans(286): host not slave E (615) spi_slave: spi_slave_queue_trans(286): host not slave E (616) spi_slave: spi_slave_queue_trans(286): host not slave E (653) spi_slave: spi_slave_queue_trans(286): host not slave E (654) spi_slave: spi_slave_queue_trans(286): host not slave E (655) spi_slave: spi_slave_queue_trans(286): host not slave E (693) spi_slave: spi_slave_queue_trans(286): host not slave E (694) spi_slave: spi_slave_queue_trans(286): host not slave E (695) spi_slave: spi_slave_queue_trans(286): host not slave E (733) spi_slave: spi_slave_queue_trans(286): host not slave E (734) spi_slave: spi_slave_queue_trans(286): host not slave E (735) spi_slave: spi_slave_queue_trans(286): host not slave E (774) spi_slave: spi_slave_queue_trans(286): host not slave E (775) spi_slave: spi_slave_queue_trans(286): host not slave E (776) spi_slave: spi_slave_queue_trans(286): host not slave E (813) spi_slave: spi_slave_queue_trans(286): host not slave E (814) spi_slave: spi_slave_queue_trans(286): host not slave E (815) spi_slave: spi_slave_queue_trans(286): host not slave
最新发布
09-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值