๋ฐฑ์ค€[JS] > 7762๋ฒˆ ์ด์ค‘ ์šฐ์„ ์ˆœ์œ„ ํ

2025. 1. 12. 17:21ยท๐Ÿ”’Algorithm

๋ฌธ์ œ๋งํฌ

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
'๐Ÿ”’Algorithm' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • ๋ฐฑ์ค€[JS] > 18870๋ฒˆ ์ขŒํ‘œ ์••์ถ•
  • ๋ฐฑ์ค€[JS] > 1620๋ฒˆ ๋‚˜๋Š”์•ผ ํฌ์ผ“๋ชฌ ๋งˆ์Šคํ„ฐ ์ด๋‹ค์†œ
  • ๋ฐฑ์ค€[node.js] > 1931๋ฒˆ
  • ๋ฐฑ์ค€[node.js] > 5430๋ฒˆ AC
devWarrior
devWarrior
  • devWarrior
    devWarrior
    devWarrior
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
    • ๐ŸงฉDev (263)
      • โญFE (34)
      • ๐Ÿ”’Algorithm (155)
      • โž•Etc. (11)
  • ๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

    • ํ™ˆ
    • ํƒœ๊ทธ
    • ๋ฐฉ๋ช…๋ก
    • ๊ธ€์“ฐ๊ธฐ
    • ๊ด€๋ฆฌ
  • ๋งํฌ

  • ๊ณต์ง€์‚ฌํ•ญ

  • ์ธ๊ธฐ ๊ธ€

  • ํƒœ๊ทธ

    nodejs
    js
    ๊ณจ๋“œ5
    ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ
    ๊ตฌํ˜„
    Algorithm
    FE
    ์ฝ”๋”ฉํ…Œ์ŠคํŠธ
    ์ž์Šค
    Lv2
    ํ‹ฐ์Šคํ† ๋ฆฌ์ฑŒ๋ฆฐ์ง€
    DFS
    ๊ทธ๋ฆฌ๋””
    javascript
    ์‹ค๋ฒ„3
    ๋ฐฑ์ค€
    react
    ์‹ค๋ฒ„4
    ์˜ค๋ธ”์™„
    ์‹ค๋ฒ„2
    leetcode
    node.js
    ํ”„๋ก ํŠธ์—”๋“œ
    ์‹ค๋ฒ„1
    ์ฝ”ํ…Œ
    Easy
    ์•Œ๊ณ ๋ฆฌ์ฆ˜
    ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค
    BFS
    dp
  • ์ตœ๊ทผ ๋Œ“๊ธ€

  • ์ตœ๊ทผ ๊ธ€

  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.3
devWarrior
๋ฐฑ์ค€[JS] > 7762๋ฒˆ ์ด์ค‘ ์šฐ์„ ์ˆœ์œ„ ํ
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”