結果

問題 No.9 モンスターのレベル上げ
ユーザー jp_stejp_ste
提出日時 2020-05-14 17:10:51
言語 JavaScript
(node v17.7.1)
結果
AC  
実行時間 1,400 ms / 5,000 ms
コード長 2,720 bytes
コンパイル時間 24 ms
使用メモリ 52,276 KB
最終ジャッジ日時 2022-12-06 09:12:27
合計ジャッジ時間 15,969 ms
ジャッジサーバーID
(参考情報)
judge12 / judge16
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 78 ms
35,996 KB
testcase_01 AC 65 ms
35,968 KB
testcase_02 AC 1,400 ms
51,684 KB
testcase_03 AC 1,033 ms
52,084 KB
testcase_04 AC 724 ms
52,180 KB
testcase_05 AC 603 ms
47,584 KB
testcase_06 AC 319 ms
47,660 KB
testcase_07 AC 124 ms
44,084 KB
testcase_08 AC 381 ms
47,792 KB
testcase_09 AC 1,341 ms
51,472 KB
testcase_10 AC 73 ms
35,844 KB
testcase_11 AC 1,359 ms
51,144 KB
testcase_12 AC 1,397 ms
51,616 KB
testcase_13 AC 1,193 ms
50,196 KB
testcase_14 AC 1,348 ms
51,504 KB
testcase_15 AC 1,108 ms
52,200 KB
testcase_16 AC 182 ms
45,672 KB
testcase_17 AC 851 ms
52,276 KB
testcase_18 AC 775 ms
51,756 KB
testcase_19 AC 168 ms
44,624 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