POJ 2259 Team Queue

来源:互联网 发布:聊城行知中学 编辑:程序博客网 时间:2024/06/13 08:48

1.题目

 
Team Queue
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 5322 Accepted: 1819

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. 
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. 

大意:
    构造一个队伍队列,其功能是:当一个元素入队时,会从头到尾检查队列中是否有它的队友,若有则当前元素紧随其后入队,若没有则
 至队尾。出队和普通队列一致

2.思路

需要按队伍数n构造n个普通队列,将入栈的元素分别入到每个队列中, 需要注意:
  1.每个队列得有标号,方面存取;List list
  2.记录每个元素与其队列号的映射;Map nm
  3.记录每个队列的先后顺序; Queue ns
  4.记录当前元素所代属的队伍是否已经由元素入栈;Set flag
    出队的时候按照记录顺序的队列ns中的队伍顺序出队,同时还需要判断当前出栈的队伍的队列是否还有元素, 若已经没有元素则将记录队伍顺序的队列ns弹出队首元素
 【注】开始的时候一定要记得将每个数据结构都clear一下

3.代码

import java.util.ArrayList;import java.util.HashMap;import java.util.HashSet;import java.util.LinkedList;import java.util.List;import java.util.Map;import java.util.Queue;import java.util.Scanner;import java.util.Set;public class Main {static List<Queue> list = new ArrayList<Queue>();//存放n个队伍的队列static Queue<Integer> ns = new LinkedList<Integer>(); //存储队列号static Map<Integer,Integer> nm = new HashMap<Integer,Integer>(); //记录元素与队列号的映射static Set<Integer> flag = new HashSet<Integer>();//标识是否有同组元素static void input(Scanner scanner,int n){int nElem,elemNum;for(int i=0;i<n;i++){String[] line = scanner.nextLine().split(" ");elemNum = Integer.parseInt(line[0]);for(int j=1;j<=elemNum;j++){nm.put(Integer.parseInt(line[j]),i);}}}public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int n ;int count = 1;while((n=Integer.parseInt(scanner.nextLine())) != 0){list.clear();ns.clear();flag.clear();nm.clear();for(int i=0;i<n;i++)list.add(new LinkedList<Integer>());input(scanner,n);System.out.println("Scenario #" + count++);String line;int elem;while(!(line=scanner.nextLine()).equals("STOP")){String[] command = line.split(" ");if(command[0].equals("ENQUEUE")){elem = Integer.parseInt(command[1]);int index = nm.get(elem);if(!flag.contains(index)){//判断当前是否有同队元素flag.add(index);ns.add(index);}list.get(index).add(elem);//进入对应队伍的队列}else{int index;if( list.get(ns.peek()).isEmpty()){flag.remove(ns.poll());index = ns.peek();}elseindex = ns.peek();System.out.println(list.get(index).poll());}}System.out.println();}}}


原创粉丝点击