結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 66 ms
39,296 KB
testcase_01 AC 61 ms
39,168 KB
testcase_02 AC 61 ms
39,040 KB
testcase_03 AC 59 ms
39,168 KB
testcase_04 AC 60 ms
39,296 KB
testcase_05 AC 82 ms
39,296 KB
testcase_06 AC 78 ms
39,168 KB
testcase_07 AC 80 ms
39,424 KB
testcase_08 AC 79 ms
39,424 KB
testcase_09 AC 81 ms
39,040 KB
testcase_10 AC 82 ms
39,296 KB
testcase_11 AC 81 ms
39,296 KB
testcase_12 AC 84 ms
39,296 KB
testcase_13 AC 62 ms
39,680 KB
testcase_14 AC 58 ms
39,040 KB
testcase_15 AC 69 ms
43,264 KB
testcase_16 AC 62 ms
39,168 KB
testcase_17 AC 59 ms
39,296 KB
testcase_18 AC 62 ms
39,424 KB
testcase_19 AC 64 ms
39,424 KB
testcase_20 AC 67 ms
43,008 KB
testcase_21 AC 61 ms
39,552 KB
testcase_22 AC 67 ms
43,392 KB
testcase_23 AC 63 ms
39,424 KB
testcase_24 AC 61 ms
39,296 KB
testcase_25 AC 69 ms
43,136 KB
testcase_26 AC 65 ms
43,008 KB
testcase_27 AC 64 ms
42,880 KB
testcase_28 AC 63 ms
39,040 KB
testcase_29 AC 62 ms
39,040 KB
testcase_30 AC 61 ms
39,168 KB
testcase_31 AC 64 ms
43,136 KB
testcase_32 AC 62 ms
39,424 KB
testcase_33 AC 69 ms
43,136 KB
testcase_34 AC 61 ms
39,040 KB
testcase_35 AC 67 ms
42,880 KB
testcase_36 AC 60 ms
39,296 KB
testcase_37 AC 59 ms
39,424 KB
testcase_38 AC 62 ms
39,424 KB
testcase_39 AC 63 ms
39,296 KB
testcase_40 AC 67 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