반응형
문제
https://www.acmicpc.net/problem/1966
1966번: 프린터 큐
여러분도 알다시피 여러분의 프린터 기기는 여러분이 인쇄하고자 하는 문서를 인쇄 명령을 받은 ‘순서대로’, 즉 먼저 요청된 것을 먼저 인쇄한다. 여러 개의 문서가 쌓인다면 Queue 자료구조에
www.acmicpc.net
풀이
const fs = require("fs");
const filePath = process.platform === "linux" ? "/dev/stdin" : "./input.txt";
let [n, ...input] = fs.readFileSync(filePath).toString().trim().split("\n");
const N = +n;
let documents = Array.from({ length: N }, () => []);
for (let i = 0; i < input.length; i++) {
documents[Math.floor(i / 2)].push(input[i].split(" ").map(Number));
}
let answer = [];
for (let document of documents) {
let cnt = 1;
const idx = document[0][1]; // 몇 번째 문서
const order = Array.from({ length: document[1].length }, (val, idx) => idx); // 순서 배열
const arr = document[1]; // 문서 배열
while (true) {
const popped = arr.shift();
const poppedOrder = order.shift();
// 해당하는 order인지 확인
const maxVal = Math.max(...arr);
if (popped >= maxVal) {
if (poppedOrder === idx) {
answer.push(cnt);
break;
}
cnt++;
} else {
arr.push(popped);
order.push(poppedOrder);
}
}
}
console.log(answer.join("\n"));
반응형
'Algorithm > BOJ' 카테고리의 다른 글
[알고리즘/백준/10798] 세로읽기 (0) | 2023.06.15 |
---|---|
[알고리즘/백준/1935] 후위 표기식2(Nodejs, 자료구조) (0) | 2023.06.14 |
[알고리즘/백준/16234] 인구이동(Nodejs, BFS) (0) | 2023.06.13 |
[알고리즘/백준/17626] Four Squares(Nodejs, DP) (0) | 2023.06.13 |
[알고리즘/백준/16439] 치킨치킨치킨(Nodejs, 완전탐색) (0) | 2023.06.12 |