結果

問題 No.198 キャンディー・ボックス2
ユーザー jp_stejp_ste
提出日時 2020-04-26 23:51:25
言語 JavaScript
(node v21.7.1)
結果
AC  
実行時間 71 ms / 1,000 ms
コード長 1,710 bytes
コンパイル時間 38 ms
コンパイル使用メモリ 5,376 KB
実行使用メモリ 39,808 KB
最終ジャッジ日時 2024-04-21 03:24:56
合計ジャッジ時間 2,980 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
39,552 KB
testcase_01 AC 67 ms
39,424 KB
testcase_02 AC 71 ms
39,680 KB
testcase_03 AC 68 ms
39,424 KB
testcase_04 AC 66 ms
39,424 KB
testcase_05 AC 65 ms
39,424 KB
testcase_06 AC 68 ms
39,680 KB
testcase_07 AC 66 ms
39,680 KB
testcase_08 AC 66 ms
39,424 KB
testcase_09 AC 65 ms
39,296 KB
testcase_10 AC 65 ms
39,680 KB
testcase_11 AC 65 ms
39,424 KB
testcase_12 AC 64 ms
39,424 KB
testcase_13 AC 66 ms
39,552 KB
testcase_14 AC 66 ms
39,808 KB
testcase_15 AC 66 ms
39,552 KB
testcase_16 AC 65 ms
39,424 KB
testcase_17 AC 66 ms
39,424 KB
testcase_18 AC 65 ms
39,552 KB
testcase_19 AC 69 ms
39,424 KB
testcase_20 AC 66 ms
39,424 KB
testcase_21 AC 66 ms
39,680 KB
testcase_22 AC 65 ms
39,296 KB
testcase_23 AC 66 ms
39,424 KB
testcase_24 AC 65 ms
39,424 KB
testcase_25 AC 69 ms
39,424 KB
testcase_26 AC 67 ms
39,680 KB
testcase_27 AC 66 ms
39,296 KB
testcase_28 AC 65 ms
39,680 KB
testcase_29 AC 69 ms
39,552 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

function main(input) {
  let [b] = getIntegers(input);
  let [n] = getIntegers(input);
  let c = input.map(x => Number(x));
  
  let low = 0;
  let high = Math.floor(c.reduce((a, b) => a + b, b) / n);
  while (high - low > 2) {
    let c1 = Math.floor((low * 2 + high) / 3);
    let c2 = Math.floor((low + high * 2) / 3);
    if (solve(c1, c) > solve(c2, c)) {
      low = c1;
    } else {
      high = c2;
    }
  }
  let min = Number.MAX_SAFE_INTEGER;
  for(let i=low; i<=high; i++) {
    min = Math.min(min, solve(i, c));
  }
  console.log(min);
}

function solve(v, c) {
  let num = 0;
  for(let i=0; i<c.length; i++) {
    num += (Math.abs(v - c[i]));
  }
  return num;
}

//-- functions ------------------------------------

function getIntegers(lines) {
  return lines.shift().split(" ").map(function(e) {
    return Number(e);
  });
}

function getStrings(lines) {
  return lines.shift().split(" ");
}

function twoDimensionalArray(h, w, value) {
  const list = new Array(h);
  for(let i=0; i<h; i++) {
    list[i] = new Array(w).fill().map(function(e) {
      return value.constructor == Array ? Array.from(value) : value;
    });
  }
  return list;
}

//-- Array -------------------------------------------

Array.prototype.pushNoSameValue = function(...values) {
  values.forEach(function(e) {
    if(!this.includes(e)) {
      this.push(e);
    }
  }, this);
};

Array.prototype.sortByIntegers = function() {
  this.sort(function(a, b) {
    return a - b;
  });
};

Array.prototype.oneLineString = function() {
  let str = "";
  this.forEach(function(e, i) {
    if(i > 0) str += " ";
    str += e;
  });
  return str;
};

main(require("fs").readFileSync("/dev/stdin", "utf8").trim().split("\n"));
0