-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrinterQueue.java
57 lines (41 loc) · 1.36 KB
/
PrinterQueue.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
package org.example.Algorithm.test;
import java.util.*;
public class PrinterQueue {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
StringBuilder sb = new StringBuilder();
int testCnt = sc.nextInt();
for (int i = 0; i < testCnt; i++) {
int N = sc.nextInt();
int M = sc.nextInt();
LinkedList<int[]> files = new LinkedList<>();
for (int j = 0; j < N; j++) {
files.add(new int[] {j, sc.nextInt()});
}
int count = 0;
while (!files.isEmpty()) {
int[] front = files.poll();
boolean isMax = true;
for (int j = 0; j < files.size(); j++) {
if (front[1] < files.get(j)[1]) {
files.add(front);
for (int k = 0; k < j; k++) {
files.add(files.poll());
}
isMax = false;
break;
}
}
if(!isMax) {
continue;
}
count++;
if(front[0] == M) {
break;
}
}
sb.append(count).append("\n");
}
System.out.println(sb);
}
}