結果
| 問題 | No.198 キャンディー・ボックス2 |
| コンテスト | |
| ユーザー |
jp_ste
|
| 提出日時 | 2020-04-26 23:51:25 |
| 言語 | JavaScript (node v23.5.0) |
| 結果 |
AC
|
| 実行時間 | 63 ms / 1,000 ms |
| コード長 | 1,710 bytes |
| コンパイル時間 | 71 ms |
| コンパイル使用メモリ | 7,840 KB |
| 実行使用メモリ | 44,160 KB |
| 最終ジャッジ日時 | 2025-11-26 11:44:17 |
| 合計ジャッジ時間 | 2,874 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 27 |
ソースコード
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"));
jp_ste