티스토리 뷰
https://www.acmicpc.net/problem/20311
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.cnt == b.cnt) {
return a.name < b.name;
} else {
return a.cnt > b.cnt;
}
};
const pq = new PriorityQueue(priority);
const [[N, K], C] = require('fs')
.readFileSync('./dev/stdin')
.toString()
.trim()
.split('\n')
.map((v) => v.split(' ').map(Number));
const answer = [];
let impossible = false;
C.forEach((c, i) => {
if (c > (N + 1) / 2) {
impossible = true;
}
pq.push({ cnt: c, name: i + 1 });
});
if (impossible) {
console.log(-1);
process.exit();
}
while (!pq.isEmpty()) {
const item1 = pq.pop();
answer.push(item1.name);
item1.cnt = item1.cnt - 1;
if (!pq.isEmpty()) {
const item2 = pq.pop();
answer.push(item2.name);
item2.cnt = item2.cnt - 1;
if (item2.cnt > 0) pq.push(item2);
}
if (item1.cnt > 0) pq.push(item1);
}
console.log(answer.join(' '));
728x90
'자료구조 알고리즘 > 백준' 카테고리의 다른 글
Node.js) 백준 20157번: 화살을 쏘자! (0) | 2023.09.14 |
---|---|
Node.js) 백준 17839번: Baba is Rabbit (0) | 2023.09.13 |
Node.js) 백준 1501번: 영어 읽기 (0) | 2023.09.13 |
Node.js) 백준 23254번: 나는 기말고사형 인간이야 (0) | 2023.09.13 |
Node.js) 백준 21773번: 가희와 프로세스 1 (0) | 2023.09.12 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 은둔청년체험
- 그래프
- 서버점검
- create databases;
- 서버개발
- MOD
- BFS
- DB 생성
- 동적프로그래밍
- 면접질문
- 면접비
- node.js
- 투포인터 연습
- 롱베케이션
- 개발자면접
- 투포인터
- 최소공통조상
- 다이나믹프로그래밍
- 다이나밍프로그래밍
- MySQL
- 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 |
글 보관함