|
| 1 | +/* |
| 2 | + 중요도는 1 ~ 9까지이므로 별도의 클래스가 아닌 배열을 통해 저장하고 |
| 3 | + 인덱스와 중요도 파악을 위한 Node 클래스 사용 |
| 4 | +*/ |
| 5 | + |
| 6 | +import java.io.*; |
| 7 | +import java.util.*; |
| 8 | + |
| 9 | +class Node { |
| 10 | + int index, priority; |
| 11 | + Node (int index, int priority) { |
| 12 | + this.index = index; |
| 13 | + this.priority = priority; |
| 14 | + } |
| 15 | +} |
| 16 | +class Main { |
| 17 | + public static void main(String[] args) throws Exception { |
| 18 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 19 | + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 20 | + int caseCount = Integer.parseInt(br.readLine()); |
| 21 | + StringBuilder result = new StringBuilder(); |
| 22 | + |
| 23 | + while (caseCount-- > 0) { |
| 24 | + Queue<Node> que = new LinkedList<>(); |
| 25 | + |
| 26 | + int[] priorities = new int[10]; |
| 27 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 28 | + |
| 29 | + int numCount = Integer.parseInt(st.nextToken()); |
| 30 | + int targetIndex = Integer.parseInt(st.nextToken()); |
| 31 | + |
| 32 | + st = new StringTokenizer(br.readLine()); |
| 33 | + |
| 34 | + for (int i = 0; i < numCount; i++) { |
| 35 | + int priority = Integer.parseInt(st.nextToken()); |
| 36 | + priorities[priority]++; |
| 37 | + que.add(new Node(i, priority)); |
| 38 | + } |
| 39 | + |
| 40 | + int count = 0; |
| 41 | + while (true) { |
| 42 | + |
| 43 | + Node node = que.poll(); |
| 44 | + |
| 45 | + if (isHighestPriority(priorities, node.priority)) { |
| 46 | + priorities[node.priority]--; |
| 47 | + count++; |
| 48 | + |
| 49 | + if (node.index == targetIndex) { |
| 50 | + result.append(count).append("\n"); |
| 51 | + break; |
| 52 | + } |
| 53 | + } else { |
| 54 | + que.offer(node); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + bw.write(result.toString()); |
| 59 | + bw.flush(); |
| 60 | + bw.close(); |
| 61 | + } |
| 62 | + |
| 63 | + public static boolean isHighestPriority(int[] priorities, int priority) { |
| 64 | + for (int i = priority + 1; i <= 9; i++) { |
| 65 | + if (priorities[i] > 0) { |
| 66 | + return false; |
| 67 | + } |
| 68 | + } |
| 69 | + return true; |
| 70 | + } |
| 71 | + |
| 72 | +} |
0 commit comments