結果

問題 No.9 モンスターのレベル上げ
ユーザー jp_stejp_ste
提出日時 2020-05-14 17:10:51
言語 JavaScript
(node v21.7.1)
結果
AC  
実行時間 1,385 ms / 5,000 ms
コード長 2,720 bytes
コンパイル時間 40 ms
コンパイル使用メモリ 6,944 KB
実行使用メモリ 52,500 KB
最終ジャッジ日時 2024-04-21 03:34:18
合計ジャッジ時間 14,199 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 61 ms
38,656 KB
testcase_01 AC 61 ms
38,912 KB
testcase_02 AC 1,216 ms
51,688 KB
testcase_03 AC 1,013 ms
51,548 KB
testcase_04 AC 714 ms
51,300 KB
testcase_05 AC 571 ms
51,304 KB
testcase_06 AC 287 ms
47,356 KB
testcase_07 AC 119 ms
45,736 KB
testcase_08 AC 339 ms
48,228 KB
testcase_09 AC 1,157 ms
52,500 KB
testcase_10 AC 57 ms
38,912 KB
testcase_11 AC 1,223 ms
51,712 KB
testcase_12 AC 1,385 ms
51,796 KB
testcase_13 AC 1,021 ms
51,560 KB
testcase_14 AC 1,159 ms
51,648 KB
testcase_15 AC 1,080 ms
52,472 KB
testcase_16 AC 154 ms
45,704 KB
testcase_17 AC 829 ms
51,816 KB
testcase_18 AC 763 ms
51,560 KB
testcase_19 AC 127 ms
45,360 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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"));
0