結果
| 問題 |
No.9 モンスターのレベル上げ
|
| ユーザー |
jp_ste
|
| 提出日時 | 2020-05-14 17:10:51 |
| 言語 | JavaScript (node v23.5.0) |
| 結果 |
AC
|
| 実行時間 | 1,414 ms / 5,000 ms |
| コード長 | 2,720 bytes |
| コンパイル時間 | 76 ms |
| コンパイル使用メモリ | 5,376 KB |
| 実行使用メモリ | 53,104 KB |
| 最終ジャッジ日時 | 2024-10-13 01:54:33 |
| 合計ジャッジ時間 | 14,646 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 20 |
ソースコード
function main(input) {
let [N] = getIntegers(input, " ");
let A = getIntegers(input, " ");
let B = getIntegers(input, " ");
let C = new Array(N).fill(0);
let min = Number.MAX_SAFE_INTEGER;
for(let i=0; i<N; i++) {
let queue = new PriorityQueue(function(a, b) {
if(a.v == b.v) return a.p < b.p;
return a.v < b.v;
});
for(let j=0; j<N; j++) {
let val = {v:A[j], p:0};
queue.push(val);
}
let now = solve(queue, B, i);
min = Math.min(min, now);
}
console.log(min);
}
function solve(Q, B, i) {
let j = i;
let max = 0;
while(true) {
let enemy = B[i];
let now = Q.pop();
now.v += Math.floor(enemy / 2);
now.p ++;
max = Math.max(max, now.p);
Q.push(now);
i = (i+1) % B.length;
if(i == j) break;
}
return max;
}
//-- functions ---------------------------------------------------------------
function getIntegers(l, s) {return s === undefined ? l.map(x=>Number(x)) : l.shift().split(s).map(x=>Number(x))}
const top = 0;
const parent = i => ((i + 1) >>> 1) - 1;
const left = i => (i << 1) + 1;
const right = i => (i + 1) << 1;
class PriorityQueue {
constructor(comparator = (a, b) => a < b) {
this._heap = [];
this._comparator = comparator;
}
size() {
return this._heap.length;
}
isEmpty() {
return this.size() == 0;
}
peek() {
return this._heap[top];
}
push(...values) {
values.forEach(value => {
this._heap.push(value);
this._siftUp();
});
return this.size();
}
pop() {
const poppedValue = this.peek();
const bottom = this.size() - 1;
if (bottom > top) {
this._swap(top, bottom);
}
this._heap.pop();
this._siftDown();
return poppedValue;
}
replace(value) {
const replacedValue = this.peek();
this._heap[top] = value;
this._siftDown();
return replacedValue;
}
_greater(i, j) {
return this._comparator(this._heap[i], this._heap[j]);
}
_swap(i, j) {
[this._heap[i], this._heap[j]] = [this._heap[j], this._heap[i]];
}
_siftUp() {
let node = this.size() - 1;
while (node > top && this._greater(node, parent(node))) {
this._swap(node, parent(node));
node = parent(node);
}
}
_siftDown() {
let node = top;
while (
(left(node) < this.size() && this._greater(left(node), node)) ||
(right(node) < this.size() && this._greater(right(node), node))
) {
let maxChild = (right(node) < this.size() && this._greater(right(node), left(node))) ? right(node) : left(node);
this._swap(node, maxChild);
node = maxChild;
}
}
}
main(require("fs").readFileSync("/dev/stdin", "utf8").trim().split("\n"));
jp_ste