AcWing 132. 小组队列(Java)
原题链接
简单
作者:
peilin
,
2020-07-07 06:11:30
,
所有人可见
,
阅读 825
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//the number of teams
int t = scanner.nextInt();
int k = 1;
while (t != 0) {
System.out.format("Scenario #%d%n", k);
int[] personToTeamId = new int[1000000];
for(int i = 1; i <= t; i++) {
int size = scanner.nextInt();
while(size-- > 0) {
personToTeamId[scanner.nextInt()] = i;
}
}
List<Queue<Integer>> queuesList = new ArrayList<>(t);
for(int i = 0; i < t; i++) {
queuesList.add(new LinkedList<>());
}
Queue<Integer> teams = new LinkedList<>();
String instruction = scanner.next();
while(!instruction.equals("STOP")) {
if(instruction.equals("ENQUEUE")) {
int personId = scanner.nextInt();
int teamId = personToTeamId[personId];
Queue<Integer> queue = queuesList.get(teamId - 1);
if(queue.isEmpty()) {
queue.offer(personId);
teams.offer(teamId);
} else {
queue.offer(personId);
}
} else {
int teamId = teams.peek();
Queue<Integer> queue = queuesList.get(teamId - 1);
System.out.println(queue.poll());
if(queue.isEmpty()) teams.poll();
}
instruction = scanner.next();
}
System.out.println();
t = scanner.nextInt();
k++;
}
}
}