結果

問題 No.275 中央値を求めよ
ユーザー jcm_2jcm_2
提出日時 2019-08-12 01:35:33
言語 JavaScript
(node v21.7.1)
結果
WA  
実行時間 -
コード長 1,449 bytes
コンパイル時間 40 ms
コンパイル使用メモリ 6,816 KB
実行使用メモリ 45,300 KB
最終ジャッジ日時 2024-04-21 02:54:56
合計ジャッジ時間 3,748 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 62 ms
39,040 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 59 ms
39,424 KB
testcase_09 AC 57 ms
39,168 KB
testcase_10 WA -
testcase_11 AC 63 ms
39,040 KB
testcase_12 WA -
testcase_13 AC 63 ms
39,168 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 63 ms
39,424 KB
testcase_17 AC 62 ms
39,424 KB
testcase_18 WA -
testcase_19 AC 62 ms
39,296 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 64 ms
39,424 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 65 ms
43,008 KB
testcase_27 AC 68 ms
43,136 KB
testcase_28 AC 63 ms
39,040 KB
testcase_29 AC 64 ms
39,424 KB
testcase_30 AC 60 ms
39,168 KB
testcase_31 WA -
testcase_32 AC 61 ms
39,424 KB
testcase_33 AC 65 ms
43,008 KB
testcase_34 AC 61 ms
39,168 KB
testcase_35 AC 66 ms
45,040 KB
testcase_36 AC 61 ms
39,296 KB
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 61 ms
39,040 KB
testcase_40 AC 68 ms
43,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

function Main(input) {
  Array.prototype.swap = function (a, b) {
    var tmp = this[a]
    this[a] = this[b]
    this[b] = tmp
    return this
  }
  function quickSort(a) {
    var recurse = function (a, i, j) {
      var k, p = pivot(a, i, j)
      if (p !== -1) {
        k = partition(a, i, j, a[p])
        recurse(a, i, k - 1)
        recurse(a, k, j)
      }
      return a
    },
      pivot = function (a, i, j) {
        var k = i + 1
        while (k <= j && a[i] === a[k]) {
          k++
        }
        if (k > j) {
          return -1
        }
        if (a[i] >= a[k]) {
          return i
        }
        return k
      },
      partition = function (a, i, j, p) {
        var l = i, r = j
        while (l <= r) {
          while (l <= j && a[l] < p) {
            l++
          }
          while (r >= i && a[r] >= p) {
            r--
          }
          if (l > r) {
            break
          }
          a.swap(l++, r--)
        }
        return l
      }
    return recurse(a, 0, a.length - 1)
  }
  
  const data = input.trim().split("\n")
  const N = parseInt(data[0])
  let array = data[1].split(" ").map(n => { return parseInt(n) })
  array = quickSort(array)
  switch (N % 2) {
    case 0:
      console.log((array[array.length / 2] + array[array.length / 2 + 1]) / 2)
      break
    case 1:
      console.log(array[~~(array.length / 2)])
      break
  }
}
Main(require("fs").readFileSync("/dev/stdin", "utf8"))
0