-
Notifications
You must be signed in to change notification settings - Fork 0
/
YourQueue.java
66 lines (61 loc) · 1.76 KB
/
YourQueue.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=3359 #heap
* #queue note: not all elements shown
*/
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Scanner;
class YourQueue {
static String calculate(int p, int c, ArrayList<String> arr) {
Deque<Integer> queue = new LinkedList<>();
for (int i = 1; i < Math.min(c, p) + 1; i++) {
queue.addLast(i);
}
StringBuilder result = new StringBuilder();
for (int idx = 0; idx < c; idx++) {
if (arr.get(idx).equals("N")) {
result.append(queue.peekFirst() + "\n");
queue.addLast(queue.pollFirst());
} else {
Scanner sc = new Scanner(arr.get(idx));
String s = sc.next();
int mark = sc.nextInt();
sc.close();
Deque<Integer> helpQ = new LinkedList<>();
while (!queue.isEmpty() && queue.peekLast() != mark) {
helpQ.addLast(queue.pollLast());
}
queue.pollLast();
while (!helpQ.isEmpty()) {
queue.addLast(helpQ.pollLast());
}
queue.addFirst(mark);
}
}
return result.toString();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int idx = 1;
StringBuilder result = new StringBuilder();
while (true) {
int p = sc.nextInt();
int c = sc.nextInt();
if (p == 0 && c == 0) {
break;
}
sc.nextLine();
ArrayList<String> arr = new ArrayList<>();
for (int i = 0; i < c; i++) {
String temp = sc.nextLine();
arr.add(temp);
}
result.append("Case " + idx + ":" + "\n");
idx++;
result.append(calculate(p, c, arr));
}
sc.close();
System.out.println(result);
}
}