|
| 1 | +// 추가 개선 예정 |
| 2 | +// 시간: 72분 |
| 3 | +// 시간 복잡도 O(N^2) |
| 4 | + |
| 5 | +import java.io.BufferedReader; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.InputStreamReader; |
| 8 | +import java.util.LinkedList; |
| 9 | +import java.util.Queue; |
| 10 | +import java.util.StringTokenizer; |
| 11 | + |
| 12 | +public class Boj1966 { |
| 13 | + public static void main(String...args) throws IOException { |
| 14 | + var br = new BufferedReader(new InputStreamReader(System.in)); |
| 15 | + var sb = new StringBuilder(); |
| 16 | + |
| 17 | + var numberOfTestCases = Integer.parseInt(br.readLine()); |
| 18 | + while(numberOfTestCases-- > 0){ |
| 19 | + var st = new StringTokenizer(br.readLine()); |
| 20 | + var length = Integer.parseInt(st.nextToken()); |
| 21 | + var targetPosition = Integer.parseInt(st.nextToken()); |
| 22 | + var count = 0; |
| 23 | + |
| 24 | + var elements = br.readLine().split(" "); |
| 25 | + Queue<Document> documents = new LinkedList<>(); |
| 26 | + |
| 27 | + // 큐 초기화 |
| 28 | + for(int i = 0 ; i< length; i++) { |
| 29 | + documents.offer(new Document(i, Integer.parseInt(elements[i]))); |
| 30 | + } |
| 31 | + |
| 32 | + // 큐에 원소가 없어질때까지 |
| 33 | + while(!documents.isEmpty()){ |
| 34 | + var document = documents.poll(); |
| 35 | + var nowDocumentPrintable = true; // 우선순위 판단 플래그 |
| 36 | + for(var doc: documents){ |
| 37 | + // 높은 우선순위를 가진 다른 문서가 있는 경우 플래그를 false 로 변경함 |
| 38 | + if (document.priority < doc.priority) { |
| 39 | + nowDocumentPrintable = false; |
| 40 | + break; |
| 41 | + } |
| 42 | + } |
| 43 | + if(nowDocumentPrintable){ |
| 44 | + count++; |
| 45 | + if(document.index == targetPosition){ |
| 46 | + break; |
| 47 | + } |
| 48 | + continue; |
| 49 | + } |
| 50 | + // 다른 우선순위가 있는 경우 다시 밀어넣음 |
| 51 | + documents.offer(document); |
| 52 | + } |
| 53 | + sb.append(count).append("\n"); |
| 54 | + } |
| 55 | + System.out.println(sb); |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | +} |
| 60 | +class Document{ |
| 61 | + public int index; |
| 62 | + public int priority; |
| 63 | + |
| 64 | + public Document(int index, int priority) { |
| 65 | + this.index = index; |
| 66 | + this.priority = priority; |
| 67 | + } |
| 68 | +} |
0 commit comments