結果

問題 No.9 モンスターのレベル上げ
ユーザー jp_stejp_ste
提出日時 2020-05-14 17:10:51
言語 JavaScript
(node v21.7.1)
結果
AC  
実行時間 1,443 ms / 5,000 ms
コード長 2,720 bytes
コンパイル時間 35 ms
コンパイル使用メモリ 5,332 KB
実行使用メモリ 82,800 KB
最終ジャッジ日時 2023-08-03 04:05:32
合計ジャッジ時間 16,886 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
42,388 KB
testcase_01 AC 81 ms
42,456 KB
testcase_02 AC 1,404 ms
81,036 KB
testcase_03 AC 1,048 ms
64,908 KB
testcase_04 AC 729 ms
52,112 KB
testcase_05 AC 608 ms
56,944 KB
testcase_06 AC 317 ms
51,896 KB
testcase_07 AC 118 ms
49,152 KB
testcase_08 AC 373 ms
52,052 KB
testcase_09 AC 1,386 ms
82,800 KB
testcase_10 AC 79 ms
42,376 KB
testcase_11 AC 1,443 ms
80,336 KB
testcase_12 AC 1,397 ms
80,100 KB
testcase_13 AC 1,206 ms
80,984 KB
testcase_14 AC 1,319 ms
80,912 KB
testcase_15 AC 1,144 ms
64,568 KB
testcase_16 AC 172 ms
50,040 KB
testcase_17 AC 904 ms
56,604 KB
testcase_18 AC 784 ms
56,604 KB
testcase_19 AC 162 ms
50,060 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