AcWing 132. 【java】小组队列
原题链接
简单
作者:
tt2767
,
2019-12-08 16:48:26
,
所有人可见
,
阅读 733
// 从queue中pop出一个组的flag时记得从set中删除
import java.util.*;
public class Main{
void run(){
int case_num = 0;
while (true){
int t = jin.nextInt();
if (t == 0) break;
if (case_num != 0) System.out.println();
case_num += 1;
System.out.printf("Scenario #%d\n", case_num);
while( t > groups.size()) groups.add(new LinkedList<Integer>());
for (int i = 0 ; i < t ; i++) groups.get(i).clear();
inque.clear();
map.clear();
que.clear();
for (int i = 0 ; i < t ; i++){
int n = jin.nextInt();
for (int j = 0 ; j < n ; j++){
int per = jin.nextInt();
map.put(per, i);
}
}
while (true){
String op = jin.next();
if (op.equals("STOP")) break;
if (op.equals("ENQUEUE")){
int per = jin.nextInt();
push(per);
} else if (op.equals("DEQUEUE")){
pop();
}
}
}
System.out.println();
}
void push(int x){
int idx = map.get(x);
groups.get(idx).offer(x);
if (! inque.contains(idx)){
que.offer(idx);
inque.add(idx);
}
}
void pop(){
int top = que.peek();
while (groups.get(top).isEmpty()){
que.poll();
inque.remove(top);
top = que.peek();
}
int per = groups.get(top).poll();
System.out.println(per);
}
Set<Integer> inque = new HashSet<>();
Map<Integer, Integer> map = new HashMap<>();
Queue<Integer> que = new LinkedList<>();
List<Queue<Integer>> groups = new ArrayList<>();
private Scanner jin = new Scanner(System.in);
public static void main(String[] args) throws Exception {new Main().run();}
}