๋ฌธ์ ๋งํฌ
https://www.acmicpc.net/problem/7662
๋ฌธ์ ํ์ด
๋๋ ์ด ๋ฌธ์ ๋ฅผ ํ๊ธฐ ์ํด ์ต๋ํ, ์ต์ํ์ ๋ง๋ค๊ณ ์ค์ ๋ก ํ์ ์กด์ฌํด์ผ ํ ์ซ์๋ค์ Map ๊ฐ์ฒด๋ก ๊ด๋ฆฌํ์๋ค.
( Primary Queue ๋ฅผ ์๋ฐ์คํฌ๋ฆฝํธ๋ก ๊ตฌํํ๊ธฐ ์ํด์ ์ง์ Class ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด์ผ ํด์ ์ด์ฉ ์ ์์ด ์ฝ๋๊ฐ ๊ธธ์ด์ง ์ ์ดํด ๋ถํ๋๋ฆฝ๋๋ค. )
์ฐ์ฐ ๊ณผ์ ์ ์ด๋ฌํ๋ค.
- I ์ฐ์ฐ์ผ ๋๋ ์ต๋ ํ, ์ต์ ํ์ ์ซ์๋ฅผ ๋ฃ์๊ณ
- D 1 ์ฐ์ฐ์ผ ๋๋ ์ต๋ํ์์ ์ต๋ ์ซ์๋ฅผ ๋ฝ๊ณ
- D -1 ์ฐ์ฐ์ผ ๋๋ ์ต์ํ์์ ์ต์ ์ซ์๋ฅผ ๋ฝ์๋ค.
- I ์ฐ์ฐ์ผ๋ก ์ซ์๋ฅผ ๋ฃ์ ๋๋ map ๊ฐ์ฒด์์ ํด๋น ์ซ์์ ๊ฐฏ์๋ฅผ +1
- D ์ฐ์ฐ์ผ๋ก ์ซ์๋ฅผ ๋บ ๋๋ map ๊ฐ์ฒด์์ ํด๋น ์ซ์์ ๊ฐฏ์๋ฅผ -1
์ด๋ฐ ๊ณผ์ ์ ๊ฑฐ์น๋ฉด ํ์ ๋ค์ด์๋ ์ซ์๊ฐ ์ค์ ๋ก ์ ํจํ ์ซ์์ธ์ง map ๊ฐ์ฒด๋ฅผ ํตํด ์ ์ ์๊ธฐ ๋๋ฌธ์ ๋ชจ๋ ์ฐ์ฐ์ด ๋๋ฌ์ ๋ ์ต๋๊ฐ, ์ต์๊ฐ์ ๊ตฌํ ์ ์๋ค.
const readline = require("readline");
const rl = readline.createInterface({
input: process.platform === "linux" ? process.stdin : fs.createReadStream("./input.text"),
output: process.stdout,
});
class Heap {
constructor(compareFn) {
this.heap = [];
this.compareFn = compareFn;
}
insert(n) {
let heap = this.heap;
heap.push(n);
if (heap.length > 1) {
let childIdx = heap.length - 1;
let parentIdx = Math.floor((childIdx - 1) / 2);
while (childIdx !== 0) {
if (this.compareFn(heap[parentIdx], heap[childIdx])) {
break;
}
[heap[parentIdx], heap[childIdx]] = [heap[childIdx], heap[parentIdx]];
childIdx = parentIdx;
parentIdx = Math.floor((childIdx - 1) / 2);
}
}
}
pop() {
let heap = this.heap;
if (heap.length === 0) {
return null;
} else if (heap.length === 1) {
return heap.pop();
} else {
let popped = heap[0];
heap[0] = heap.pop();
let parentIdx = 0;
while (parentIdx * 2 + 1 < heap.length) {
let childIdx = parentIdx * 2 + 1;
if (childIdx + 1 < heap.length && this.compareFn(heap[childIdx + 1], heap[childIdx])) {
childIdx += 1;
}
if (this.compareFn(heap[parentIdx], heap[childIdx])) {
break;
}
[heap[parentIdx], heap[childIdx]] = [heap[childIdx], heap[parentIdx]];
parentIdx = childIdx;
}
return popped;
}
}
}
let maxHeap = new Heap((parent, child) => {
if (parent > child) {
return true;
} else {
return false;
}
});
let minHeap = new Heap((parent, child) => {
if (parent < child) {
return true;
} else {
return false;
}
});
let map = new Map();
let answer = "";
let index = 0;
let opCnt = 0;
let lastOpIndex = 0;
rl.on("line", (line) => {
if (index === 0) {
++index;
return;
}
if (line.split(" ").length === 1) {
maxHeap.heap = [];
minHeap.heap = [];
map.clear();
lastOpIndex = index + Number(line);
++index;
return;
}
let [op, v] = line.split(" ");
if (op === "I") {
let insertNum = Number(v);
maxHeap.insert(insertNum);
minHeap.insert(insertNum);
if (map.has(insertNum)) {
map.set(insertNum, map.get(insertNum) + 1);
} else {
map.set(insertNum, 1);
}
} else {
if (v === "1") {
while (maxHeap.heap.length) {
let popped = maxHeap.pop();
if (map.get(popped) >= 1) {
map.set(popped, map.get(popped) - 1);
break;
}
}
} else if (v === "-1") {
while (minHeap.heap.length) {
let popped = minHeap.pop();
if (map.get(popped) >= 1) {
map.set(popped, map.get(popped) - 1);
break;
}
}
}
}
if (index === lastOpIndex) {
let max = undefined;
while (maxHeap.heap.length) {
let popped = maxHeap.pop();
if (map.get(popped) >= 1) {
max = popped;
break;
}
}
let min = undefined;
while (minHeap.heap.length) {
let popped = minHeap.pop();
if (map.get(popped) >= 1) {
min = popped;
break;
}
}
if (max !== undefined || min !== undefined) {
max = max !== undefined ? max : min;
min = min !== undefined ? min : max;
answer += `${max} ${min}\n`;
} else {
answer += "EMPTY\n";
}
}
++index;
});
rl.on("close", () => {
console.log(answer.trim());
});
๋ฌธ์ ํ๊ธฐ
๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ต์ ํ๋ฅผ ์ํด fs ๋ชจ๋์ด ์๋ readline์ ์ด์ฉํ๊ณ ์ต๋ํ, ์ต์ํ์ ํ๋์ Heap ํด๋์ค์ ํ๋์ ์ธ์๋ฅผ ์ฃผ์ด ๊ฐ๊ฐ์ ์ธ์คํด์ค๋ฅผ ๋ง๋ค์๋ค. ์ฒ์ ์ ํด๋ณด๋ ๋ฌธ์ ์ ํ์ด๋ผ ๊ณ ๋ฏผํ๋ ์๊ฐ์ ๋ง์ด ๊ฐ์ก๊ณ ๋ค๋ฅธ ๋ถ๋ค์ ํ์ด๋ ์ฐธ๊ณ ํ๋ ์ข์ ์๊ฐ์ด์๋ค.
'๐Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฐฑ์ค[JS] > 18870๋ฒ ์ขํ ์์ถ (0) | 2025.01.18 |
---|---|
๋ฐฑ์ค[JS] > 1620๋ฒ ๋๋์ผ ํฌ์ผ๋ชฌ ๋ง์คํฐ ์ด๋ค์ (0) | 2025.01.13 |
๋ฐฑ์ค[node.js] > 1931๋ฒ (0) | 2025.01.09 |
๋ฐฑ์ค[node.js] > 5430๋ฒ AC (0) | 2025.01.07 |
๋ฐฑ์ค[node.js] > 9461๋ฒ ํ๋๋ฐ ์์ด (0) | 2025.01.05 |