結果

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

テストケース

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

ソースコード

diff #

function main(input) {
  let [b] = getIntegers(input);
  let [n] = getIntegers(input);
  let c = [];
  for(let i=0; i<n; i++) {
    c.push(Number(input.shift()));
  }
  c.sort((a, b) => b - a);
  
  let ans = 0;
  if(n > 1) {
    if(n % 2 == 1) {
      const index = Math.round(n / 2) - 1;
      ans = solve(c[index], b, c);
    } else {
      const index1 = Math.round(n / 2) - 1;
      const index2 = Math.round(n / 2);
      ans = Math.min(solve(c[index1], b, c), solve(c[index2], b, c));
    }
  }
  console.log(ans);
}

function solve(v, b, c) {
  let num = 0;
  for(let i=0; i<c.length; i++) {
    if(c[i] > v) {
      b += (c[i] - v);
      num += (c[i] - v);
    } else if(c[i] < v) {
      b -= (v - c[i]);
      num += (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").split("\n"));
0