티스토리 뷰

https://www.acmicpc.net/problem/21773

 

21773번: 가희와 프로세스 1

1초일 때 부터 4초일 때 상황을 그림으로 나타내면 아래와 같습니다. 아래 그림에서 주황색은 특정 시점에 스케쥴러가 선택한 프로세스를 의미합니다.

www.acmicpc.net

class PriorityQueue {
	constructor(priority) {
		this.heap = [];
		this.pairIsInCorrectOrder = priority;
	}

	getLeftChildIndex(parentIndex) {
		return 2 * parentIndex + 1;
	}

	getRightChildIndex(parentIndex) {
		return 2 * parentIndex + 2;
	}

	getParentIndex(childIndex) {
		return Math.floor((childIndex - 1) / 2);
	}

	hasParent(childIndex) {
		return this.getParentIndex(childIndex) >= 0;
	}

	hasLeftChild(parentIndex) {
		return this.getLeftChildIndex(parentIndex) < this.heap.length;
	}

	hasRightChild(parentIndex) {
		return this.getRightChildIndex(parentIndex) < this.heap.length;
	}

	leftChild(parentIndex) {
		return this.heap[this.getLeftChildIndex(parentIndex)];
	}

	rightChild(parentIndex) {
		return this.heap[this.getRightChildIndex(parentIndex)];
	}

	parent(childIndex) {
		return this.heap[this.getParentIndex(childIndex)];
	}

	swap(indexA, indexB) {
		const tmp = this.heap[indexA];
		this.heap[indexA] = this.heap[indexB];
		this.heap[indexB] = tmp;
	}

	peek() {
		return this.heap.length == 0 ? null : this.heap[0];
	}

	isEmpty() {
		return !this.heap.length;
	}

	pop() {
		if (this.heap.length == 0) {
			return null;
		}

		if (this.heap.length == 1) {
			return this.heap.pop();
		}

		const item = this.heap[0];

		this.heap[0] = this.heap.pop();
		this.bubbleDown();
		return item;
	}

	push(item) {
		this.heap.push(item);
		this.bubbleUp();
		return this;
	}

	bubbleUp() {
		let currentIndex = this.heap.length - 1;

		while (
			this.hasParent(currentIndex) &&
			!this.pairIsInCorrectOrder(this.parent(currentIndex), this.heap[currentIndex])
		) {
			this.swap(currentIndex, this.getParentIndex(currentIndex));
			currentIndex = this.getParentIndex(currentIndex);
		}
	}

	bubbleDown() {
		let currentIndex = 0;
		let nextIndex = null;

		while (this.hasLeftChild(currentIndex)) {
			if (
				this.hasRightChild(currentIndex) &&
				this.pairIsInCorrectOrder(this.rightChild(currentIndex), this.leftChild(currentIndex))
			) {
				nextIndex = this.getRightChildIndex(currentIndex);
			} else {
				nextIndex = this.getLeftChildIndex(currentIndex);
			}
			if (this.pairIsInCorrectOrder(this.heap[currentIndex], this.heap[nextIndex])) {
				break;
			}
			this.swap(currentIndex, nextIndex);
			currentIndex = nextIndex;
		}
	}
}
const priority = (a, b) => {
	if (a.priority > b.priority) return true;
	else if (a.priority < b.priority) return false;
	else {
		if (a.id < b.id) return true;
		else return false;
	}
};

const pq = new PriorityQueue(priority);
const readline = require('readline').createInterface({
	input: process.stdin,
	output: process.stdout,
});
const answer = [];
let T, n;
readline.on('line', (line) => {
	const input = line.split(' ').map(Number);
	if (input.length == 2) {
		[T, n] = input;
	} else {
		const [id, time, priority] = input;
		pq.push({
			id,
			time,
			priority,
		});
	}
}).on('close', () => {
	let t = 0;
	while (t < T) {
		let now = pq.pop();
		answer.push(now.id);
		now.time -= 1;
		now.priority -= 1;
		if (now.time > 0) {
			pq.push(now);
		}
		t++;
	}

	console.log(answer.join('\n'));
	process.exit();
});
728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/10   »
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
글 보관함