티스토리 뷰
https://www.acmicpc.net/problem/11000
11000번: 강의실 배정
첫 번째 줄에 N이 주어진다. (1 ≤ N ≤ 200,000) 이후 N개의 줄에 Si, Ti가 주어진다. (0 ≤ Si < Ti ≤ 109)
www.acmicpc.net
class PriorityQueue {
constructor(callback) {
this.heap = [];
this.callback = callback;
}
swap(a, b) {
const temp = { ...this.heap[a] };
this.heap[a] = this.heap[b];
this.heap[b] = temp;
}
size() {
return this.heap.length;
}
getParentIndex(cur) {
return Math.floor((cur - 1) / 2);
}
push(value) {
this.heap.push(value);
let current = this.size() - 1;
let parent = this.getParentIndex(current);
while (parent >= 0 && this.callback(value, this.heap[parent])) {
this.swap(parent, current);
current = parent;
parent = this.getParentIndex(current);
}
}
pop() {
const last = this.size() - 1;
let current = 0;
this.swap(current, last);
const value = this.heap.pop();
while (current < last) {
const left = current * 2 + 1;
const right = current * 2 + 2;
if (left >= last) {
break;
} else if (right >= last) {
if (this.callback(this.heap[left], this.heap[current])) {
this.swap(current, left);
current = left;
} else {
break;
}
} else {
if (
this.callback(this.heap[left], this.heap[current]) ||
this.callback(this.heap[right], this.heap[current])
) {
const next = this.callback(this.heap[left], this.heap[right]) ? left : right;
this.swap(current, next);
current = next;
} else {
break;
}
}
}
return value;
}
}
class MinHeap {
constructor() {
this.heap = [];
}
swap(a, b) {
[this.heap[a], this.heap[b]] = [this.heap[b], this.heap[a]];
}
size() {
return this.heap.length;
}
push(value) {
this.heap.push(value);
let current = this.heap.length - 1;
let parent = Math.floor((current - 1) / 2);
while (this.heap[parent] > value) {
this.swap(parent, current);
current = parent;
parent = Math.floor((current - 1) / 2);
}
}
pop() {
const last = this.heap.length - 1;
let current = 0;
this.swap(current, last); // 0번이 루트노드
const value = this.heap.pop();
while (current < last) {
let left = current * 2 + 1;
let right = current * 2 + 2;
if (left >= last) {
break;
} else if (right >= last) {
if (this.heap[current] > this.heap[left]) {
this.swap(current, left);
current = left;
} else {
break;
}
} else {
if (this.heap[left] < this.heap[current] || this.heap[right] < this.heap[current]) {
let next = this.heap[left] < this.heap[right] ? left : right;
this.swap(current, next);
current = next;
} else {
break;
}
}
}
return value;
}
}
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout,
});
let N;
const pq = new PriorityQueue((a, b) => {
if (b.s >= a.s) return true;
else return false;
});
readline.on('line', function (line) {
const nums = line.split(' ').map(Number);
if (nums.length != 1) {
const [s, t] = nums;
pq.push({ s: s, t: t });
}
}).on('close', function () {
const minHeap = new MinHeap();
while (pq.size() > 0) {
const { s, t } = pq.pop();
if (minHeap.size() == 0) {
minHeap.push(t);
} else {
const head = minHeap.heap[0];
if (s >= head) {
minHeap.pop();
}
minHeap.push(t);
}
}
console.log(minHeap.size());
process.exit();
});
728x90
'자료구조 알고리즘 > 백준' 카테고리의 다른 글
Node.js) 백준 1662번: 압축 (0) | 2023.08.31 |
---|---|
Node.js) 백준 1351번: 무한 수 (0) | 2023.08.31 |
Node.js) 백준 1717번: 집합의 표현 (0) | 2023.08.30 |
Node.js) 백준 1247번: 부호 (0) | 2023.08.30 |
Node.js) 백준 1100번: 하얀 칸 (0) | 2023.08.29 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- create databases;
- 최소공통조상
- 은둔청년체험
- 면접비
- 로드나인
- BFS
- MOD
- 동적프로그래밍
- 다이나믹프로그래밍
- 서버점검
- 투포인터
- 면접질문
- DB 생성
- 롱베케이션
- 다이나밍프로그래밍
- 서버개발
- 투포인터 연습
- 개발자면접
- MySQL
- 그래프
- node.js
- create db
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함