티스토리 뷰
https://www.acmicpc.net/problem/23559
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;
}
}
const [[N, X], ...BOB] = require('fs')
.readFileSync('./dev/stdin')
.toString()
.trim()
.split('\n')
.map((v) => v.split(' ').map(Number));
const callback = (a, b) => {
const diffA = Math.abs(a.A - a.B);
const diffB = Math.abs(b.A - b.B);
if (diffA > diffB) return true;
else if (diffA < diffB) return false;
else {
if (a.A > b.A) return true;
else return false;
}
};
class Menu {
constructor(a, b) {
this.A = a;
this.B = b;
}
}
const pq = new PriorityQueue(callback);
BOB.forEach((v) => {
const menu = new Menu(v[0], v[1]);
pq.push(menu);
});
let DON = X / 1000;
let answer = 0;
while (pq.size() > 0) {
const { A, B } = pq.pop();
if (A - B > 0) {
if (DON >= 5 && DON - 5 >= pq.size()) {
answer += A;
DON -= 5;
} else {
answer += B;
DON -= 1;
}
} else {
answer += B;
DON -= 1;
}
}
console.log(answer);
728x90
'자료구조 알고리즘 > 백준' 카테고리의 다른 글
Node.js) 백준 25556번: 포스택 (0) | 2023.09.08 |
---|---|
Node.js) 백준 22252번: 정보 상인 호석 (0) | 2023.09.07 |
Node.js) 백준 23834번 : 콘센트 (0) | 2023.09.06 |
Node.js) 백준 7511번: 소셜 네트워킹 어플리케이션 (0) | 2023.09.05 |
Node.js) 백준 1354번: 무한 수열 2 (0) | 2023.09.05 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 서버개발
- 면접비
- 다이나밍프로그래밍
- 동적프로그래밍
- 롱베케이션
- create databases;
- BFS
- 투포인터 연습
- 로드나인
- 면접질문
- 은둔청년체험
- MySQL
- MOD
- 그래프
- 개발자면접
- create db
- DB 생성
- 투포인터
- 다이나믹프로그래밍
- 서버점검
- 최소공통조상
- node.js
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함