티스토리 뷰
// 알고리즘
1. 시뮬레이션
2. 큐를 이용한 문제
-> 인덱스와 중요도를 하나의 클래스로 정의하여 큐에 집어넣음
-> 하나씩 poll 하고 남아있는 문서의 중요도를 확인 후 처리
-> 중요도를 담는배열 arr와 해당 문서를 처리하면 -1로 설정
더보기
import java.util.*;
import java.io.*;
public class 프린터큐_1966 {
static int T, N, M, arr[], ans;
static class Point {
int idx, pr;
public Point(int idx, int pr) {
this.idx = idx;
this.pr = pr;
}
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
T = Integer.parseInt(br.readLine());
for (int tc = 0; tc < T; tc++) {
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
ans = 0;
arr = new int[N];
Queue<Point> q = new LinkedList<>();
st = new StringTokenizer(br.readLine(), " ");
for (int i = 0; i < N; i++) {
int num = Integer.parseInt(st.nextToken());
q.add(new Point(i, num));
arr[i] = num;
}
// 알고리즘
System.out.println(cal(q));
}
}
private static int cal(Queue<Point> q) {
int cnt = 1;
Point p = null;
L: while (!q.isEmpty()) {
p = q.poll();
int num = p.pr;
for (int i = 0; i < arr.length; i++) {
if (num < arr[i]) {
q.add(p);
continue L;
}
}
if (p.idx == M) {
return cnt;
}
cnt++;
arr[p.idx] = -1;
}
return 0;
}
}
※ 자료구조 관련 문제를 다뤄보고싶어서 해당 문제를 선택 큐는 bfs탐색할 때 자주써봐서 그런지 어렵지 않게 풀었음. 다음에는 스택관련 문제나 map관련 문제를 다뤄볼 예정
'Algorithm' 카테고리의 다른 글
백준_2048 (Easy)_12100 (0) | 2021.01.28 |
---|---|
백준_부분수열의 합_14225 (0) | 2021.01.25 |
백준_연구소_14502 (0) | 2021.01.23 |
백준_스도미노쿠_4574 (0) | 2021.01.20 |
백준_벽 부수고 이동하기 2_14442 (0) | 2021.01.19 |