-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBOJ_PrinterQueue.js
79 lines (72 loc) · 2.13 KB
/
BOJ_PrinterQueue.js
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* @desc site : Baekjoon Onlie Judge
* @desc level: S3
* @desc url : https://www.acmicpc.net/problem/1966
*/
const fs = require("fs");
const { off } = require("process");
const input = fs.readFileSync("/dev/stdin").toString().trim().split("\n");
// const input = fs.readFileSync("../dummy.txt").toString().trim().split("\n");
const count = input.length;
let seq = 0;
let printCount = 0;
let targetImp = 1;
let targetSeq = 0;
for (let index = 1; index < count; index++) {
if (index % 2) {
seq = Number(input[index].split(" ")[1]);
} else {
printSequence(input[index].split(" "));
seq = 0;
printCount = 0;
targetSeq = 0;
targetImp = 1;
}
}
function printSequence(printArray) {
const targetImp = Number(printArray[seq]);
let sequence = 0;
const filterPrintArray = printArray
.map((item, index, array) => {
const importance = Number(item);
if (importance >= targetImp) {
sequence += 1;
if (index === seq) {
targetSeq = sequence - 1;
}
return (array[index] = {
pos: sequence - 1,
importance: importance,
});
}
})
.filter((item) => {
return item !== undefined;
});
while (true) {
const element = filterPrintArray[0];
const position = element.pos;
const importance = element.importance;
if (importance === getMaxValue(filterPrintArray).importance) {
printCount++;
filterPrintArray.shift();
if (position === targetSeq) {
break;
}
} else {
//가장 뒤에 다시 배치
filterPrintArray.shift();
filterPrintArray.push({
pos: position,
importance: importance,
});
}
}
console.log(printCount);
}
function getMaxValue(array) {
const max = array.reduce(function (prev, current) {
return prev.importance > current.importance ? prev : current;
});
return max;
}